按钮大小和颜色变化

时间:2015-08-29 09:38:52

标签: jquery button colors size toggle

我这里有4个按钮,但只有第一个按钮正在工作,我希望相同的jquery代码稍微更改一下,以便在所有这些按钮上工作... 我可以复制过去jquery 4次并将其更改为有效,但我希望更简单,更少的代码与一个jquery如何做到这一点?

CSS

.yellowBackground {    
    background-color: yellow;    
}

.redBackground {    
    background-color: red;
}

button.yellowBackground {    
    width:50px; 
    height:50px; 
}

HTML

<table>
    <tr>
      <td><button id="button"  class="yellowBackground">Pause</button></td>
      <td><button id="button"  class="yellowBackground">Pause</button></td>
      <td><button id="button"  class="yellowBackground">Pause</button></td>
      <td><button  id="button"  class="yellowBackground">Pause</button></td>
   </tr>
</table>

JS

$(function() {    
    $("#button").click(function() {    
        changeThumb();    
    });    
});

function changeThumb() {    
    $("#button").toggleClass("redBackground");    
}

3 个答案:

答案 0 :(得分:1)

adb shell monkey -p com.package.name -c android.intent.category.LAUNCHER 1 必须是唯一的,您也可以使用类选择器ID执行此操作,如下所示:

yellowBackground

FIDDLE DEMO

此外,您错过了在$(function() { $(".yellowBackground").click(function() { $(this).toggleClass("redBackground"); }); }); html中打开<tr>标记。请更正

答案 1 :(得分:0)

ID必须是唯一的,您可以将处理程序添加到类.yellowBackgroundbutton

$(function() {

    $(".yellowBackground").click(function() {

        $(this).toggleClass("redBackground");

    });

});

答案 2 :(得分:-1)

在JQuery选择中选择类或id之前指定元素:

$(function() {

$('button#button').click(function() {

        $(this).toggleClass("redBackground");

    });

});
相关问题