为什么我的盒子的颜色在提示和确认时没有变化?

时间:2017-12-12 16:02:40

标签: javascript html css

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 个答案:

答案 0 :(得分:0)

这对我有用。

<强>解释

  1. 我注意到JavaScript无法从element.style类读取.white,但是当我将内联CSS插入每个元素时可以这样做。这在以下jsfiddle中有详细说明。

  2. 您在函数中使用了this范围,而没有实际提供this实际上是什么。所以,我在this事件处理程序中传递了一个click参数,由于this不能用作函数参数,因此将其更改为e

  3. &#13;
    &#13;
    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;
    &#13;
    &#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代码,即使我复制并粘贴了代码片段也无法正常工作