Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.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 由于有许多按钮,如何更改按钮背景色_Javascript_Jquery_Css_Dom - Fatal编程技术网

Javascript 由于有许多按钮,如何更改按钮背景色

Javascript 由于有许多按钮,如何更改按钮背景色,javascript,jquery,css,dom,Javascript,Jquery,Css,Dom,我已经创建了很多HTML按钮,当用户按下它时,它应该改变背景颜色或者做一些事情让用户知道它。然而,我并没有在一开始就创建那个事件。 现在我遇到了麻烦,我应该在每个触发事件中添加大量指令来更改颜色,还是有其他更方便的简单方法。 我想创建一个公共函数来接收按钮id,用它来控制应该突出显示哪个按钮。尽管如此,我不知道如何获得如下按钮id: $("#upload").bind("click", function(){ //I want to get the string 'upload' })

我已经创建了很多HTML按钮,当用户按下它时,它应该改变背景颜色或者做一些事情让用户知道它。然而,我并没有在一开始就创建那个事件。

现在我遇到了麻烦,我应该在每个触发事件中添加大量指令来更改颜色,还是有其他更方便的简单方法。

我想创建一个公共函数来接收按钮id,用它来控制应该突出显示哪个按钮。尽管如此,我不知道如何获得如下按钮id:

$("#upload").bind("click", function(){

 //I want to get the string 'upload'
 })
$("button").bind("click", function(){
   var thisID = $(this).attr('id'); //this is the id

   if (thisID == "upload") {
      $(this).css({"background":"black","color":"white"});
   }
});
还有更方便的方法吗?我不想添加太多的说明 对于许多按钮。

事件处理程序方法中的此项将引用单击的按钮,因此您可以使用this.id获取id

这会将单击按钮的背景更改为蓝色

演示:

但我可能更喜欢使用css类来实现这一点

button {
    background-color: lightgrey;
}

button.active {
    background-color: blue;
}

var buttons = $(":button").bind("click", function(){
    buttons.filter('.active').removeClass('active')
    $(this).addClass('active')
})
演示:

这就是你想要的吗

HTML


对于您的具体问题,如果您想要ID,您可以这样做:

$("#upload").bind("click", function(){
   var upload = $(this).attr('id'); //this is the id
})
关键是.attr'id'

正如其他海报所提到的,如果你想做的只是改变背景颜色,你甚至不需要这个。但是,如果您想要一个只处理您确定的某些ID的函数,您甚至可以执行以下操作:

$("#upload").bind("click", function(){

 //I want to get the string 'upload'
 })
$("button").bind("click", function(){
   var thisID = $(this).attr('id'); //this is the id

   if (thisID == "upload") {
      $(this).css({"background":"black","color":"white"});
   }
});

干杯,希望有帮助。

您考虑过使用CSS吗?处理程序方法中的这一点指向button@mohkhan实际上,如果我能得到按钮id,我会使用它~.cssbackground color,蓝色;每次只能突出显示一个按钮。您好,我创建了一个名为controlBtnColors.JS的单独JS文件,它的“内容是var buttons=。。。但它不能工作。我很确定我包含了这个js文件。有点奇怪。当我在Chrome中使用命令行时,它可以工作…对不起,我知道了。你是对的。我使用$document.readyfunction{};解决这个愚蠢的问题。DOM元素需要在事件准备好后绑定它们……我不知道我可以在代码中使用css类设计。css设计很漂亮。顺便说一句:请保留小提琴。我想探索更多,谢谢。
$(":button").bind("click", function(){
            iid=this.id;
                if(iid=="upload")
                {
                    $(this).css('background-color', 'blue')
                }
                else if(iid=="upload1")
                {
                    $(this).css('background-color', 'yellow')
                }
                else
                {

                }
            });
    });
$(":button").bind("click", function(){
            iid=this.id;
                if(iid=="upload")
                {
                    $(this).css('background-color', 'blue')
                }
                else if(iid=="upload1")
                {
                    $(this).css('background-color', 'yellow')
                }
                else
                {

                }
            });
    });