Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用java脚本更改第二个按钮的颜色_Javascript_Html - Fatal编程技术网

Javascript 使用java脚本更改第二个按钮的颜色

Javascript 使用java脚本更改第二个按钮的颜色,javascript,html,Javascript,Html,我有两个红色按钮。如果单击按钮,颜色应变为绿色。如果再次单击,它将返回红色。 现在我成功地更改了第一个按钮的颜色,但没有更改第二个按钮的颜色。 有人有主意吗 我已经有了一个java脚本,可以更改一个按钮的颜色 var-button=document.querySelector(“按钮”); addEventListener(“单击”,函数(){ const curcolor=button.style.backgroundColor; 如果(curcolor===“红色”){ button.st

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

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

var-button=document.querySelector(“按钮”);
addEventListener(“单击”,函数(){
const curcolor=button.style.backgroundColor;
如果(curcolor===“红色”){
button.style.backgroundColor=“绿色”;
}否则{
button.style.backgroundColor=“红色”;
}
});
按钮{
高度:40px;
宽度:160px;
边界:4px;
边界半径:20px 20px 20px 20px;
边框颜色:红色;
颜色:黄色;
填充:12px 15px;
文本对齐:居中;
字体大小:16px;
光标:指针;
}
.按钮1{
背景:红色
}
.按钮1:悬停{
背景颜色:绿色;
}
概述


图版
您可以使用
切换类
。同时使用
querySelectorAll
这将提供所有按钮。然后迭代此集合并向其中添加事件侦听器。内部回调函数使用
classList.toggle
添加或删除类

var按钮=[…document.queryselectoral(“按钮”)].forEach((项)=>{
item.addEventListener(“单击”,函数(){
this.classList.toggle('toggleButtonColor')
});
})
按钮{
高度:40px;
宽度:160px;
边界:4px;
边界半径:20px 20px 20px 20px;
边框颜色:红色;
颜色:黄色;
填充:12px 15px;
文本对齐:居中;
字体大小:16px;
光标:指针;
}
.按钮1{
背景:红色
}
.按钮1:悬停{
背景颜色:绿色;
}
.切换按钮颜色{
背景:绿色;
}

一般的

盘子
您可以使用
切换类
。同时使用
querySelectorAll
这将提供所有按钮。然后迭代此集合并向其中添加事件侦听器。内部回调函数使用
classList.toggle
添加或删除类

var按钮=[…document.queryselectoral(“按钮”)].forEach((项)=>{
item.addEventListener(“单击”,函数(){
this.classList.toggle('toggleButtonColor')
});
})
按钮{
高度:40px;
宽度:160px;
边界:4px;
边界半径:20px 20px 20px 20px;
边框颜色:红色;
颜色:黄色;
填充:12px 15px;
文本对齐:居中;
字体大小:16px;
光标:指针;
}
.按钮1{
背景:红色
}
.按钮1:悬停{
背景颜色:绿色;
}
.切换按钮颜色{
背景:绿色;
}

一般的

盘子
您使用两种不同的方法来处理单击。使用
onclick
addEventListener
。这里有一个例子

我还没有改进你函数的内部。请查看@brk答案中的选项

函数showOrHide(id、名称){
const button=document.getElementById(id);
const curcolor=button.style.backgroundColor;
如果(curcolor==“红色”| |!curcolor){
button.style.backgroundColor='绿色';
}否则{
button.style.backgroundColor='红色';
}
}
按钮{
高度:40px;
宽度:160px;
边界:4px;
边界半径:20px 20px 20px 20px;
边框颜色:红色;
颜色:黄色;
填充:12px 15px;
文本对齐:居中;
字体大小:16px;
光标:指针;
}
.按钮1{
背景色:红色;
}
.按钮1:悬停{
背景颜色:绿色;
}
身体{
背景色:#E36
}

一般的


板材
您正在使用两种不同的方法来处理单击。使用
onclick
addEventListener
。这里有一个例子

我还没有改进你函数的内部。请查看@brk答案中的选项

函数showOrHide(id、名称){
const button=document.getElementById(id);
const curcolor=button.style.backgroundColor;
如果(curcolor==“红色”| |!curcolor){
button.style.backgroundColor='绿色';
}否则{
button.style.backgroundColor='红色';
}
}
按钮{
高度:40px;
宽度:160px;
边界:4px;
边界半径:20px 20px 20px 20px;
边框颜色:红色;
颜色:黄色;
填充:12px 15px;
文本对齐:居中;
字体大小:16px;
光标:指针;
}
.按钮1{
背景色:红色;
}
.按钮1:悬停{
背景颜色:绿色;
}
身体{
背景色:#E36
}

一般的

铭牌
您可以使用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> 
概述

盘子
您可以使用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> 
概述

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

var buttons = document.querySelectorAll("button");
for (const button of buttons) {
    // ...
}
var buttons=document.querySelectorAll(“按钮”);
用于(按钮中的常量按钮){
addEventListener(“单击”,函数(){
const curcolor=button.style.backgroundColor;
如果(curcolor===“红色”){
button.style.backgroundColor=“绿色”;
}否则{
button.style.backgroundColor=“红色”;
}
});
}
按钮{
高度:40px;
宽度:160px;
边界:4px;
边界半径:20px 20px 20px 20px;
边框颜色:红色;
颜色:黄色;
填充:12px 15px;
文本对齐:居中;
字体大小:16px;
光标:指针;
}
.按钮1{
背景:红色
}
.按钮1:悬停{
背景颜色:绿色;
}
概述


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

var buttons = document.querySelectorAll("button");
for (const button of buttons) {
    // ...
}
var buttons=document.querySelectorAll(“按钮”);
用于(按钮中的常量按钮){
addEventListener(“单击”,函数(){
const curcolor=button.style.backgroundColor;
如果(