jQuery 中的多个 else if 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8514053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple else if Statements in jQuery
提问by Alex
$('#bar3').live('click', function() {
if($('#bar3').attr('class') == '0') {
console.log("closed")
} else if($('#bar1' && '#bar2').attr('class') == '0') {
console.log("Both")
} else if($('#bar1').attr('class') == '0') {
console.log("Summary Open")
} else if($('#bar2').attr('class') == '0') {
console.log("HIP Open")
} else {
console.log("open")
}
});
Why does this not work?
为什么这不起作用?
I know what it's doing, I just don't know what to change to fix it.
我知道它在做什么,我只是不知道要改变什么来修复它。
This part is wrong:
这部分是错误的:
else if($('#bar1' && '#bar2').attr('class') == '0')
回答by u710480
$('#bar1' && '#bar2').attr('class') == '0'
should be
应该
$('#bar1').attr('class') == "0" && $('#bar2').attr('class') == "0"
&& is a valid java script operator, but not valid as part of a jquery selector.
&& 是一个有效的 java 脚本运算符,但作为 jquery 选择器的一部分无效。
回答by Jon
Because you are simply using the wrong syntax. This:
因为您只是使用了错误的语法。这个:
$('#bar1' && '#bar2').attr('class') == '0'
would need to be written more like
需要写得更像
$('#bar1').attr('class') == '0' && $('#bar2').attr('class') == '0'
But that still leaves major issues:
但这仍然留下了主要问题:
- What is
.attr('class') == '0'
supposed to do? Do you have a class named0
? If so, the correct way would be.hasClass('0')
. - You keep re-wrapping DOM elements in jQuery objects. Do it only once and store the results, e.g.
var $bar1 = $('#bar1')
and then use$bar1
again and again.
- 什么是
.attr('class') == '0'
应该做的?你有一个名为 的类0
吗?如果是这样,正确的方法是.hasClass('0')
。 - 您不断在 jQuery 对象中重新包装 DOM 元素。只做一次并存储结果,例如
var $bar1 = $('#bar1')
,然后$bar1
一次又一次地使用。
回答by Dmitriy
They aren't actually hardcoded. I grabbed that code from the the property inspector and not my HTML file, sorry bout that! The class is dynamically added by TinyAccordion script – Alex Dec 15 '11 at 2:19
它们实际上不是硬编码的。我从属性检查器中获取了该代码,而不是我的 HTML 文件,抱歉!该类由 TinyAccordion 脚本动态添加 – Alex 2011 年 12 月 15 日 2:19
You only need to change in TinyAccordion this code:
您只需要在 TinyAccordion 中更改此代码:
var parentAccordion=new TINY.accordion.slider("parentAccordion");
parentAccordion.init("acc","h3",0,0);
to this:
对此:
var parentAccordion=new TINY.accordion.slider("parentAccordion");
parentAccordion.init("acc","h3",0,0,"your_class_name");
回答by redShadow
Try replacing
尝试更换
} else if($('#bar1' && '#bar2').attr('class') == '0') {
with:
和:
} else if ( ($('#bar1').attr('class') == '0') && ($('#bar2').attr('class') == '0')) {
UPDATE:
更新:
I just tried this code, that is working on jsfiddle:
我刚刚试过这段代码,它在 jsfiddle 上工作:
$('#bar3').live('click', function() {
if ($('#bar1').hasClass("0") && $('#bar2').hasClass("0")) {
alert("Both bars are Open");
} else if ($('#bar1').hasClass("0")) {
alert("Bar 1 is Open");
} else if ($('#bar2').hasClass("0")) {
alert("Bar 2 is Open");
} else {
alert("No bars are open");
}
});
回答by Matt Lacey
You're not checking two conditions, you're using && with two strings, and passing the result of that to jQuery. You want to do this:
您没有检查两个条件,而是将 && 与两个字符串一起使用,并将其结果传递给 jQuery。你想这样做:
else if($('#bar1').attr('class') == '0' && $('#bar2').attr('class') == '0')