检查jQuery中是否为undefined和null

编程入门 行业动态 更新时间:2024-10-10 04:29:04
本文介绍了检查jQuery中是否为undefined和null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在检查是否为null或undefined以及是否应该使用!==或!=以及"undefined"或undefined时,我有些困惑.

I'm a bit confused when checking if null or undefined and if I should be using !== or != and "undefined" or undefined.

这是我正在处理的一些代码.我的null/undefined哪里出错了?

Here is some code I'm working on. Where am I going wrong with my null/unudefined etc?

var c = (jQuery(this).prop("target") != null && jQuery(this).prop("target") != undefined && jQuery(this).prop("target").toLowerCase() == "_blank") ? 1 : 0;

谢谢

推荐答案

  • null和undefined都是假"值,因此可以像检查布尔值一样对其进行检查.因此,将null和undefined进行比较是没有意义的,除了某些情况下,您需要知道它们是否是这样的值.

    • Both null and undefined are "falsy" values, thus they can be checked like they were boolean values. Thus, there's no sense comparing to null and undefined except for certain situations where you need to know if they are such values.

      进行比较时,最好使用严格的比较(例如===,!==等)

      when comparing, it's best to use strict comparison (like ===,!== and so on)

      在条件中的&&如果前面的条件是虚假",则不会评估以下条件.

      the && in a condition does not evaluate the following condition if the one preceeding it is "falsy".

      您甚至不需要jQuery ,因为this是您的DOM对象(大概是<a>),而您正在尝试获取target属性:

      You don't even need jQuery since this is your DOM object (presumably an <a>) and you are trying to get the target property:

      最后:

      var c = (this.target && this.target.toLowerCase() === "_blank") ? 1 : 0;

更多推荐

检查jQuery中是否为undefined和null

本文发布于:2023-11-09 11:47:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572294.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:jQuery   undefined   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!