用javascript更改颜色第二个按钮

问题描述 投票:0回答:6

我有两个红色按钮。如果单击按钮,颜色应更改为绿色。如果再次单击它应该返回红色。现在我成功更改了第一个按钮的颜色,但没有更改第二个按钮的颜色。有人有想法吗?

我已经有一个java脚本可以改变一个按钮的颜色

var button = document.querySelector("button");

button.addEventListener("click", function() {
  const curColour = button.style.backgroundColor;

  if (curColour === 'red') {
    button.style.backgroundColor = "green";
  } else {
    button.style.backgroundColor = "red";
  }
});
button {
  height: 40px;
  width: 160px;
  border: 4px;
  border-radius: 20px 20px 20px 20px;
  border-color: red;
  color: yellow;
  padding: 12px 15px;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
}

.button1 {
  background: red
}

.button1:hover {
  background-color: green;
}
<button id="bGeneral" class="button1" name="bGeneral"><b>General</b></button>
<!-- Create extra space -->
<p><br></p>
<!-- The Next Button Plates -->
<button id="buttonP" class="button1" name="buttonP"><b>Plates</b></button>

期望能够改变两个按钮的颜色

javascript html
6个回答
0
投票

正如安德烈亚斯在评论中已经说过的那样你需要querySelectorAll而不是querySelector。这将为您提供一个必须循环的集合

var buttons = document.querySelectorAll("button");
for (const button of buttons) {
    // ...
}

var buttons = document.querySelectorAll("button");

for (const button of buttons) {
  button.addEventListener("click", function() {
    const curColour = button.style.backgroundColor;

    if (curColour === 'red') {
      button.style.backgroundColor = "green";
    } else {
      button.style.backgroundColor = "red";
    }
  });
}
button {
  height: 40px;
  width: 160px;
  border: 4px;
  border-radius: 20px 20px 20px 20px;
  border-color: red;
  color: yellow;
  padding: 12px 15px;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
}

.button1 {
  background: red
}

.button1:hover {
  background-color: green;
}
<button id="bGeneral" class="button1" name="bGeneral"><b>General</b></button>
<!-- Create extra space -->
<p><br></p>
<!-- The Next Button Plates -->
<button id="buttonP" class="button1" name="buttonP"><b>Plates</b></button>

2
投票

你可以使用toggleClass。也可以使用querySelectorAll这将给出所有按钮。然后迭代此集合并向其添加事件侦听器。 .Inside回调函数使用classList.toggle添加或删除类

var button = [...document.querySelectorAll("button")].forEach((item) => {
  item.addEventListener("click", function() {
    this.classList.toggle('toggleButtonColor')
  });

})
button {
  height: 40px;
  width: 160px;
  border: 4px;
  border-radius: 20px 20px 20px 20px;
  border-color: red;
  color: yellow;
  padding: 12px 15px;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
}

.button1 {
  background: red
}

.button1:hover {
  background-color: green;
}

.toggleButtonColor {
  background: green;
}
<body style="background-color:#E3CEF6;">

  <button id="bGeneral" class="button1" name="bGeneral"><b>General</b></button>
  <!-- Create extra space -->
  <p><br></p>
  <!-- The Next Button Plates -->
  <button id="buttonP" class="button1" name="buttonP"><b>Plates</b></button>
</body>

0
投票

您正在使用两种不同的方式来处理点击。使用onclickaddEventListener。这是一个例子。

我没有改进你的功能内部。看看opb的@brk答案

function showOrHide(id, name) {
  const button = document.getElementById(id);

  const curColour = button.style.backgroundColor;
  
  if (curColour === 'red' || !curColour) {
    button.style.backgroundColor = 'green';
  } else {
    button.style.backgroundColor = 'red';
  }
}
button {
  height: 40px;
  width: 160px;
  border: 4px;
  border-radius: 20px 20px 20px 20px;
  border-color: red;
  color: yellow;
  padding: 12px 15px;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
}

.button1 {
  background-color: red;
}

.button1:hover {
  background-color: green;
}

body {
  background-color: #E3CEF6
}
<button id="bGeneral" onclick="showOrHide(this.id, 'General')" class="button1" name="bGeneral">
<b>General</b></button>
<!-- Create extra space -->
<p><br></p>

<!-- The Next Button Plates -->
<button id="buttonP" onclick="showOrHide(this.id, 'Plates')" class="button1" name="buttonP"><b>Plates</b></button>

0
投票

你可以使用id:

function showOrHide(id) {
  var element = document.getElementById(id);
  const curColour = element.style.backgroundColor;
  if (curColour === 'red') {

        element.style.backgroundColor = "green";
    }
    else {
        element.style.backgroundColor = "red";
    }
}

并在HTML中:

<button  id="bGeneral" onclick="showOrHide(this.id)" class="button1"  name= "bGeneral" ><b>General</b></button> 
  <!-- Create extra space -->
<p><br></p> 
<!-- The Next Button Plates -->
<button id = "buttonP" onclick="showOrHide(this.id)" class="button1" name= "buttonP" ><b>Plates</b></button> 

0
投票

这是你思想的正确方法

<!DOCTYPE html>
    <html>
    <head>
     <style>
    button {
      height:40px;
      width:160px;
      border: 4px;
      border-radius: 20px 20px 20px 20px;
      border-color:red;
      color: yellow;
      padding: 12px 15px;
      text-align: center;
      font-size: 16px;
      cursor: pointer;
    }
    .button1 { background: red }

    .button1:hover {
      background-color: green;
    }

    </style>
    </head>
     <body onload="beginfase()" style="background-color:#E3CEF6;" >

    <button  id="bGeneral" onclick="" class="button1"  name= "bGeneral" ><b>General</b></button> 
      <!-- Create extra space -->
    <p><br></p> 
    <!-- The Next Button Plates -->
    <button id = "buttonP" onclick="" class="button1" name= "buttonP" ><b>Plates</b></button> 

    <script type="text/javascript">
    //we are creating an array
    var button = [];
    button = document.querySelectorAll("button");
    //we are binding the function to all the elements of the array
    for(i=0;i<button.length;i++){
        button[i].onclick = function(){
    // this represent the elemement which is being clicked
          if(this.style.backgroundColor === "red"){
            this.style.backgroundColor = "green"
          }
          else{
          this.style.backgroundColor = "red"
          }

        }
    }

    </script>
      </body>

0
投票

尝试使用onclick方法,并使用querySelectorAll选择所有按钮

HTML

<button  id="bGeneral" onclick="changeColor(this)" class="button1"  name= "bGeneral" ><b>General</b></button> 
      <!-- Create extra space -->
    <p><br></p> 
    <!-- The Next Button Plates -->
    <button id = "buttonP" onclick="changeColor(this)" class="button1" name= "buttonP" ><b>Plates</b></button> 

JS

function changeColor (value) {
    var buttons = document.querySelectorAll(".button1");
    let color = value.style.backgroundColor;
    for (let i = 0; i < buttons.length; i++) {
      if (color === 'red') {
        buttons[i].style.backgroundColor = 'green'
      } else {
        buttons[i].style.backgroundColor = 'red'
      }
    }
  }
© www.soinside.com 2019 - 2024. All rights reserved.