Jquery“如果这个和如果那个”然后这样做
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18216291/
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
Jquery "if this and if that" then do this
提问by LBF
This should be so simple, but it's not working for me. I want to say:
这应该很简单,但它对我不起作用。我想说:
If this doesn't have the class "current" AND if the body class does not equal "home", then do this....
如果这没有“当前”类,并且身体类不等于“家”,则执行此操作....
Here is what I'm trying (among other things) to no avail. Only the first conditional works.
这是我正在尝试的(除其他外)无济于事。只有第一个条件有效。
$(".nav1 > ul > li").mouseleave(function() {
if ( (!$(this).hasClass("current")) || (!$(body).hasClass("home")) ){
$(this).find(".nav-overlay").show();
}
});
What am I doing wrong? Thank you!
我究竟做错了什么?谢谢!
UPDATE FOR CLARITY:
更新的清晰度:
I only want to show ".nav-overlay" when the following conditions are true:
当以下条件为真时,我只想显示“.nav-overlay”:
- You are not on the homepage (body class="home") AND
- The ".nav1 > ul > li" class does not equal "current"
- 您不在主页上(body class="home")并且
- “.nav1 > ul > li”类不等于“current”
回答by Cherniv
Try $("body")
and &&
:
尝试$("body")
和&&
:
if ( (!$(this).hasClass("current")) && (!$("body").hasClass("home")) ){
//...
Or this way , it is shorter:
或者这样,它更短:
if ( !$(this).is(".current") && !$("body").is(".home") ){
//...
回答by Ionic? Biz?u
If body
is nota variable, access it as a HTML tag: add quotes. Also, AND
is &&
instead of ||
(that means OR
):
如果body
是不变量,访问它作为一个HTML标签:添加引号。此外,AND
是&&
而不是||
(这意味着OR
):
if (!$(this).hassClass("current") && !$("body").hasClass("home")) {
$(this).find(".nav-overlay").show();
}
If body
is a jQuery object you can do directly ... && !$body.hashClass(...)
.
如果body
是 jQuery 对象,则可以直接执行... && !$body.hashClass(...)
。