Javascript 如何否定“if”语句中的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6684051/
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
How to negate code in "if" statement
提问by krzyhub
For example if I want to do something if parent element for used element hasn't got ul
as next element what should I add to this code? Somehow I try some combination of .not()
and/or
.is()
but they fail for me.
例如,如果我想做某事,如果使用元素的父元素没有ul
作为下一个元素,我应该向此代码添加什么?不知何故,我尝试了.not()
和/或的
一些组合,.is()
但它们对我来说失败了。
So someone maybe knows what is the best method for negate code after if
?
所以有人可能知道之后否定代码的最佳方法是if
什么?
if ($(this).parent().next().is('ul')){
// code...
}
回答by Justin Ethier
You can use the Logical NOT !
operator:
您可以使用逻辑非!
运算符:
if (!$(this).parent().next().is('ul')){
Or equivalently (see comments below):
或等效地(见下面的评论):
if (! ($(this).parent().next().is('ul'))){
For more information, see the Logical Operatorssection of the MDN docs.
有关更多信息,请参阅MDN 文档的逻辑运算符部分。
回答by Chandu
Try negation operator !
before $(this)
:
!
在之前尝试否定运算符$(this)
:
if (!$(this).parent().next().is('ul')){