//Css樣式<style>.first{padding:10px 0px 0px 20px;;width:300px;height:50px;background-color:#339999;}span{width:14px;height:14px;float:left;}.jia{background-image:url(+.jpg);}.jian{background-image:url(-.jpg);}</style>//jQuery代碼<script src="JQ.js" type="text/javascript"></script><script>$(function(){$(".first").toggle(function(){$(".first>span").addClass("jian");},function(){$(".first>span").addClass("jia");});});</script></head><body><div class="first"><span class="jia"></span></div></body></html>初始化:第一次點擊效果圖:再次點擊還是這是為什么,說下原因,為什么會這樣,謝謝各位大俠了
1 回答

幕布斯7119047
TA貢獻1794條經驗 獲得超8個贊
不要用addClass,因為用addClass之后,會讓對象的class="jia jian",這樣圖片肯定是不會變的。
你應該改成attr,比如上面兩句:
$(".first>span").addClass("jian"); 改成 $(".first>span").attr("class", "jian");
$(".first>span").addClass("jia"); 改成 $(".first>span").attr("class", "jia");
這樣class就會在class="jia"和class="jian"之間互相切換了。
- 1 回答
- 0 關注
- 336 瀏覽
相關問題推薦
添加回答
舉報
0/150
提交
取消