jquery中使用类选择题改变div背景色

2025-02-26 21:19:35
推荐回答(2个)
回答1:

$(".test").onclick = function(){
$(this).css({"background": "red"});
};

改为:

$(".test").click = function(){
$(this).css({"background-color": "red"});
};

jquery所有的事件绑定都没有on这个关键字。

另外,jquery的事件绑定需要放到readyfunction中去。

具体来说







style="background-color: gray; width: 300px; height: 200px;">


回答2:

把JS代码改成这样

$(document).ready(function() {
$(".test").click(function(){
$(this).css({"background-color": "red"});
});
});