jQuery-单击更改颜色 [英] jQuery - Change color on click
本文介绍了jQuery-单击更改颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在看一些jQuery,因为我想创建一个div,当您单击它时它会改变颜色.
I'm looking at some jQuery because I want to create a div that changes color when you click it.
我已经完成了:
$(function() {
$('.star').click(function(){
$(this).css('background', 'yellow');
});
});
它完美地工作! 但是,当您再次单击它时,我希望它删除背景色.那有可能吗,你会怎么做?
And it works perfectly! But I want it to remove the background color, when you click it one more time. Is that possible, and how would you do something like that?
推荐答案
创建CSS类:
.clicked {
background-color: yellow;
}
,然后只需通过jQuery 切换该类即可:
and then simply toggle that class it via jQuery:
$('.star').click(function(){
$(this).toggleClass('clicked');
});
这篇关于jQuery-单击更改颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文