我想创建 31 个按钮,并可以选择更改每个按钮的背景颜色。问题是当我尝试更改按钮 2 的颜色时,它会更改按钮 1 的颜色。
我想做的就是在图片 《十字》中。
function myFunctionRed()
{
document.getElementById("myBtn").style.background = "green";
}
function myFunctionGreen()
{
document.getElementById("myBtn").style.background = "yellow";
}
function myFunctionBlue()
{
document.getElementById("myBtn").style.background = "red";
}
// Get the modal
var modal = document.getElementById("myModal");
var modal2 = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
var btn2 = document.getElementById("myBtn2");
// Get the <span> element that closes the modal
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function()
{
modal.style.display = "block";
}
btn2.onclick = function()
{
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function()
{
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event)
{
if (event.target == modal)
{
modal.style.display = "none";
}
}
<button id="myBtn">1</button>
<button id="myBtn2">2</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<button id="demo1" onclick="myFunctionRed()">Red</button>
<button id="demo2" onclick="myFunctionGreen()">Green</button>
<button id="demo3" onclick="myFunctionBlue()">Blue</button>
</div>
</div>
小唯快跑啊
一只甜甜圈
汪汪一只猫
相关分类