如何否定“if"中的代码JavaScript -JQuery 中的语句块,例如“如果不是那么.." [英] How to negate code in "if" statement block in JavaScript -JQuery like 'if not then..'

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

问题描述

例如,如果我想在 used 元素的父元素没有 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..
}

推荐答案

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

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天全站免登陆