如何取消"if"中的代码JavaScript -JQuery中的语句块,如“如果不是,则为.." [英] How to negate code in "if" statement block in JavaScript -JQuery like 'if not then..'

查看:114
本文介绍了如何取消"if"中的代码JavaScript -JQuery中的语句块,如“如果不是,则为.."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我想做某事,如果已使用元素的父元素没有将ul标签作为下一个元素,我该如何实现呢?

For example if I want to do something if parent element for used element hasn't got ul tag as next element, how can I achieve this?

我尝试使用.not()和/或 .is()没有成功.

I try some combination of .not() and/or .is() with no success.

否定if else块代码的最佳方法是什么?

What's the best method for negate code of a if else block?

我的代码

if ($(this).parent().next().is('ul')){
   // code...
}

我想实现这一目标

伪代码:

if ($(this).parent().next().is NOT ('ul')) {
    //Do this..
}

推荐答案

您可以使用逻辑NOT !运算符:

You can use the Logical NOT ! operator:

if (!$(this).parent().next().is('ul')){

或等效地(请参见下面的评论):

Or equivalently (see comments below):

if (! ($(this).parent().next().is('ul'))){

有关更多信息,请参见MDN的逻辑运算符部分文档.

For more information, see the Logical Operators section of the MDN docs.

这篇关于如何取消"if"中的代码JavaScript -JQuery中的语句块,如“如果不是,则为.."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆