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
的跟进答案 0 :(得分:0)
这对我有用。
<强>解释强>
我注意到JavaScript无法从element.style
类读取.white
,但是当我将内联CSS插入每个元素时可以这样做。这在以下jsfiddle中有详细说明。
您在函数中使用了this
范围,而没有实际提供this
实际上是什么。所以,我在this
事件处理程序中传递了一个click
参数,由于this
不能用作函数参数,因此将其更改为e
。
function myFunction(e) {
if (confirm("Press a button!") == true) {
if (e.style.backgroundColor == "white")
e.style.backgroundColor = "yellow";
else if (e.style.backgroundColor == "yellow")
e.style.backgroundColor = "red";
else if (e.style.backgroundColor == "red")
e.style.backgroundColor = "white"
else
e.style.backgroundColor = "white";
} 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", function() {
myFunction(this);
});
}
&#13;
.foo {
float: left;
width: 30px;
height: 30px;
margin: 5px;
border: 1px solid rgba(0, 0, 0, .2);
border-radius: 25%;
}
.whole {
float: left;
width: 900px;
height: 900px;
margin: 5px;
border: 1px solid rgba(0, 0, 0, .2);
}
&#13;
<div class="whole">
<div id="centerbox1" class="foo" style="background:white;">A1</div>
<div id="centerbox2" class="foo" style="background:white;">A2</div>
<div id="centerbox3" class="foo" style="background:white;">A3</div><br><br>
<div id="centerbox4" class="foo" style="background:white;">B1</div>
<div id="centerbox5" class="foo" style="background:white;">B2</div>
<div id="centerbox6" class="foo" style="background:white;">B3</div>
</div>
&#13;
答案 1 :(得分:0)
function myFunction(e) {
if (confirm("Press a button!") == true) {
if (e.style.backgroundColor == "white")
e.style.backgroundColor = "yellow";
else if (e.style.backgroundColor == "yellow")
e.style.backgroundColor = "red";
else if (e.style.backgroundColor == "red")
e.style.backgroundColor = ""
else
e.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", function() {
myFunction(this);
});
}
.foo {
float: left;
width: 30px;
height: 30px;
margin: 5px;
border: 1px solid rgba(0, 0, 0, .2);
border-radius: 25%;
}
.white {
background-color: white;
}
.whole {
float: left;
width: 900px;
height: 900px;
margin: 5px;
border: 1px solid rgba(0, 0, 0, .2);
}
.purple {
background: #ab3fdd;
}
.wine {
background: #ae163e;
}
<div class="whole">
<div id="centerbox1" class="foo" style="background:white;">A1</div>
<div id="centerbox2" class="foo" style="background:white;">A2</div>
<div id="centerbox3" class="foo" style="background:white;">A3</div><br><br>
<div id="centerbox4" class="foo" style="background:white;">B1</div>
<div id="centerbox5" class="foo" style="background:white;">B2</div>
<div id="centerbox6" class="foo" style="background:white;">B3</div>
</div>
这是我寻求的答案!
<!doctype html>
<html>
<head>
<script language ="javascript">
function myFunction(e) {
if (confirm("Press a button!") == true) {
if (e.style.backgroundColor == "white")
e.style.backgroundColor = "yellow";
else if (e.style.backgroundColor == "yellow")
e.style.backgroundColor = "red";
else if (e.style.backgroundColor == "red")
e.style.backgroundColor = ""
else
e.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", function() {
myFunction(this);
});
}
</script>
<style type="text/css">
.foo {
float: left;
width: 30px;
height: 30px;
margin: 5px;
border: 1px solid rgba(0, 0, 0, .2);
border-radius: 25%;
}
.white {
background-color: white;
}
.whole {
float: left;
width: 900px;
height: 900px;
margin: 5px;
border: 1px solid rgba(0, 0, 0, .2);
}
.purple {
background: #ab3fdd;
}
.wine {
background: #ae163e;
}
</style>
</head>
<body>
<div class="whole">
<div id="centerbox1" class="foo" style="background:white;">A1</div>
<div id="centerbox2" class="foo" style="background:white;">A2</div>
<div id="centerbox3" class="foo" style="background:white;">A3</div><br><br>
<div id="centerbox4" class="foo" style="background:white;">B1</div>
<div id="centerbox5" class="foo" style="background:white;">B2</div>
<div id="centerbox6" class="foo" style="background:white;">B3</div>
</div>
</body>
</html>
这是我的html代码,即使我复制并粘贴了代码片段也无法正常工作