【发布时间】:2018-05-26 09:13:57
【问题描述】:
function myFunction() {
if (confirm("Press a button!") == true) {
if(this.style.backgroundColor == "white")
this.style.backgroundColor = "yellow";
else if(this.style.backgroundColor == "yellow")
this.style.backgroundColor = "red";
else if(this.style.backgroundColor == "red")
this.style.backgroundColor = ""
else
this.style.backgroundColor = "";
} else {
txt = "You pressed Cancel!";
}
if (confirm('Are you sure you want to save this thing into the database?') == true) {
// Save it!
} else {
// Do nothing!
}
}
var blocks = document.getElementsByClassName("foo");
for (i = 0; i < blocks.length; i++) {
blocks[i].addEventListener("click", myFunction);
}
.foo {
float: left;
width: 30px;
height: 30px;
margin: 5px;
border: 1px solid rgba(0, 0, 0, .2);
border-radius: 25%;
}
.white{
background: #FFFFFF;
}
.whole {
float: left;
width: 900px;
height: 900px;
margin: 5px;
border: 1px solid rgba(0, 0, 0, .2);
}
<div class="whole">
<div id="centerbox1" class="foo white">A1</div>
<div id="centerbox2" class="foo white">A2</div>
<div id="centerbox3" class="foo white">A3</div><br><br>
<div id="centerbox4" class="foo white">B1</div>
<div id="centerbox5" class="foo white">B2</div>
<div id="centerbox6" class="foo white">B3</div>
</div>
示例:我点击了正方形,它提示我是或否,颜色变成黄色。点击黄色方块会提示我另一个是或否。把它变回原来的颜色。 这个问题是div onclick prompts yes or no, if yes, div change to different color的后续问题
【问题讨论】:
-
我不知道你在问什么或问题是什么。单击一个正方形会显示 2 个提示,并且没有一个框会变成黄色。描述你想要做什么是一个更好的主意,而不是说“这不起作用”
-
您正在使用 CSS 类设置背景颜色。尝试使用内联样式。
标签: javascript html css