有没有办法从一个事件中运行多个if else语句?例如,试图运行它,但它只对类为"two“的元素执行第一个if else,而不是对类为"three”的第二个元素执行。
$(".hamburger-menu").click(function(){
if ($(".two").hasClass("two-rotate")){
$(".two").addClass("two-rotate-back").removeClass("two-rotate");
}
else {
$(".two").addClass("two-rotate").removeClass("two-rotate-back");
}
// This one won't execute
if ($(".three").hasClass("three-rotate")){
$(".three").addClass("three-rotate-back").removeClass("three-rotate");
}
else {
$(".three").addClass("three-rotate").removeClass("three-rotate-back");
}
});我知道答案一定很简单,但是我已经找了30分钟了,还是找不到答案。
发布于 2015-06-06 17:51:31
末尾缺少});,而且单击事件可能未正确绑定。
下面是工作参数r :"http://plnkr.co/edit/6hBXDFLXoGkHFBgvmbbx?p=preview“
发布于 2015-06-06 17:56:53
您没有正确关闭您的代码。
将});放在末尾。
当你需要调试一些代码时,你可以这样做:
if ( someExpression ) {
// logic here...
console.log( 'passed through if' );
} else {
// logic here...
console.log( 'passed through else' );
}发布于 2015-06-06 19:47:51
我知道您已经解决了问题,但我只想提一下,如果-rotate和-rotate-back类看起来像您的代码中那样是互斥的,那么您可以将这些if语句替换为几个简短的.toggleClass语句:
$(".hamburger-menu").click(function(){
$(".two").toggleClass("two-rotate-back two-rotate");
$(".three").toggleClass("three-rotate-back three-rotate");
});http://jsfiddle.net/UselessCode/hc4Ldm9n/
https://stackoverflow.com/questions/30681243
复制相似问题