当用户单击“共享”按钮时,我想更改我的按钮类。为此,我正在使用toggleClasses()
方法。
但是当我尝试时,它只给了我最后一节课。我哪里出错了,我该如何解决?
$('#fb_button').each(function () {
var count = 0;
var thisButton = $(this);
thisButton.click(function () {
count++;
thisButton.val("Share" + '(' + count + ')');
thisButton.toggleClass('green-button blue-button red-button')
})
})
像下面这样做: -
var array = ['green-button', 'blue-button', 'red-button'];
var count = 0;
$('#fb_button').click(function () {
if(count==3){
count = 0;
}
$(this).removeClass().addClass(array[count]);
count++;
});
.green-button{
background:green;
}
.blue-button{
background:blue;
}
.red-button{
background:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="fb_button">Click Me!</button>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句