猿问

jq2 里没有toggle方法了,有什么好的替代吗

jq2 里没有toggle方法了,有什么好的替代吗,还有事件解绑用哪个

沧海一幻觉
浏览 715回答 1
1回答

慕仙森

用hasClass/addClass/removeClass或attr标记状态实现toggle切换<button id="animate" status="0">animate</button><div id="block">Hello World</div>$("#animate").click(function(){&nbsp; &nbsp; if(!$(this).hasClass("clicked")) {&nbsp; &nbsp; &nbsp; &nbsp; $(this).addClass("clicked");&nbsp; &nbsp; &nbsp; &nbsp; $("#block").animate({"font-size": "10em"}, 1000, "linear", function(){console.log("complete");});&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $(this).removeClass("clicked");&nbsp; &nbsp; &nbsp; &nbsp; $("#block").animate({"font-size": "1em"}, 1000, "linear", function(){console.log("complete");});&nbsp; &nbsp; }});$("#animate").click(function(){&nbsp; &nbsp; if($(this).attr("status") == 0) {&nbsp; &nbsp; &nbsp; &nbsp; $(this).attr("status", 1);&nbsp; &nbsp; &nbsp; &nbsp; $("#block").animate({"font-size": "10em"}, 1000, "linear", function(){console.log("complete");});&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $(this).attr("status", 0);&nbsp; &nbsp; &nbsp; &nbsp; $("#block").animate({"font-size": "1em"}, 1000, "linear", function(){console.log("complete");});&nbsp; &nbsp; }});
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答