本文翻译自:Check if a div exists with jquery [duplicate]

Possible Duplicate: 可能重复:
Is there an “exists” function for jQuery 是否存在jQuery的“存在”功能

Yes, I know this has been asked a lot. 是的,我知道这已被问到很多。 But, it confuses me, since the results on google for this search show different methods (listed below) 但是,它让我困惑,因为谷歌搜索结果显示不同的方法(如下所列)

$(document).ready(function() {if ($('#DivID').length){alert('Found with Length');}if ($('#DivID').length > 0 ) {alert('Found with Length bigger then Zero');}if ($('#DivID') != null ) {alert('Found with Not Null');}
});

Which one of the 3 is the correct way to check if the div exists? 3中哪一个是检查div是否存在的正确方法?

EDIT: It's a pitty to see that people do not want to learn what is the better approach from the three different methods. 编辑:看到人们不想从三种不同的方法中学习什么是更好的方法,这是一个很小的问题。 This question is not actually on "How to check if a div exists" but it's about which method is better, and, if someone could explain, why it it better? 这个问题实际上并不是“如何检查div是否存在”,而是关于哪种方法更好,如果有人可以解释,为什么它更好?


#1楼

参考:https://stackoom.com/question/Swn1/检查div是否存在jquery-重复


#2楼

The first is the most concise, I would go with that. 第一个是最简洁的,我会去那。 The first two are the same, but the first is just that little bit shorter, so you'll save on bytes. 前两个是相同的,但第一个只是那么短,所以你将节省字节。 The third is plain wrong, because that condition will always evaluate true because the object will never be null or falsy for that matter. 第三个是完全错误的,因为该条件将始终评估为真,因为该对象永远不会为null或为此。


#3楼

As karim79 mentioned, the first is the most concise. 正如karim79所提到的,第一个是最简洁的。 However I could argue that the second is more understandable as it is not obvious/known to some Javascript/jQuery programmers that non-zero/false values are evaluated to true in if-statements. 但是我可以说第二个更容易理解,因为一些Javascript / jQuery程序员并不明白/知道在if语句中非零/ false值被评估为true And because of that, the third method is incorrect. 因此,第三种方法是不正确的。


#4楼

If you are simply checking for the existence of an ID, there is no need to go into jQuery , you could simply: 如果您只是检查是否存在ID,则无需进入jQuery ,您可以简单地:

if(document.getElementById("yourid") !== null)
{
}

getElementById returns null if it can't be found. 如果找不到getElementById返回null

Reference . 参考 。

If however you plan to use the jQuery object later i'd suggest: 但是,如果您打算稍后使用jQuery对象,我建议:

$(document).ready(function() {var $myDiv = $('#DivID');if ( $myDiv.length){//you can now reuse  $myDiv here, without having to select it again.}});

A selector always returns a jQuery object, so there shouldn't be a need to check against null (I'd be interested if there is an edge case where you need to check for null - but I don't think there is). 选择器总是返回一个jQuery对象,所以不需要检查null (如果有一个需要检查null的边缘情况,我会感兴趣 - 但我认为没有)。

If the selector doesn't find anything then length === 0 which is "falsy" (when converted to bool its false). 如果选择器没有找到任何东西,则length === 0 ,这是“falsy”(当转换为bool时为false)。 So if it finds something then it should be "truthy" - so you don't need to check for > 0. Just for it's "truthyness" 因此,如果它找到了某些东西,那么它应该是“真实的” - 所以你不需要检查> 0.只是为了它的“真实性”

检查div是否存在jquery [重复]相关推荐

  1. jQuery hasAttr检查元素是否有属性[重复]

    本文翻译自:jQuery hasAttr checking to see if there is an attribute on an element [duplicate] Possible Dup ...

  2. 你如何检查选择器是否匹配jQuery中的内容? [重复]

    本文翻译自:How do you check if a selector matches something in jQuery? [duplicate] This question already ...

  3. 您如何轻松地水平居中 div 使用CSS? [重复]

    本文翻译自:How do you easily horizontally center a using CSS? [duplicate] This question already has an an ...

  4. 检查是否已使用jQuery选中复选框

    如何检查是否使用复选框数组的ID选中了复选框数组中的复选框? 我正在使用以下代码,但是无论ID为何,它始终返回已选中复选框的数量. function isCheckedById(id) {alert( ...

  5. html div findchild,javascript – JQuery查找父div子标签

    参见英文答案 > jQuery: How to get to a particular child of a parent?                                    ...

  6. html中js隐藏div的高度,jQuery实现获取隐藏div高度的方法示例

    jQuery实现获取隐藏div高度的方法示例 发布时间:2020-09-22 19:27:55 来源:脚本之家 阅读:95 作者:zhuyangxing 本文实例讲述了jQuery实现获取隐藏div高 ...

  7. html中隐藏div的高度,jQuery如何获取隐藏元素的高度?

    一个HTML元素可以在jquery的hide()函数的帮助下隐藏,或者可以通过在css中使用visibility:hidden;来轻松隐藏.我们也可以很容易地使用jquery来找到这个隐藏元素的高度. ...

  8. 设置div圆角的jquery插件-jquery.corner.js

    代码 /*!  * jQuery corner plugin: simple corner rounding  * Examples and documentation at: http://jque ...

  9. html jq固定div高度,js/jQuery获取/设置div的高度,宽度···

    js/jQuery获取/设置html标签的高度等css属性· $(".out-div").height($(window).height() - $("#topDiv&q ...

最新文章

  1. Android基础总结+SQlite数据库【申明:来源于网络】
  2. tkinter笔记:scale 尺度 (莫烦python笔记)
  3. python3爬虫初探(一)之urllib.request
  4. 你真的了解CSS3硬件加速吗?
  5. OAM深入解读:使用OAM定义与管理Kubernetes内置Workload
  6. Linux系统编程19:基础IO之了解Linux中的标准输入和输出以及相关的系统调用接口(如write,read等)
  7. CSS基础「二」复合选择器 / 元素显示模式 / 背景 / 三大特性
  8. 力扣53.最大子序和 多种方法
  9. Linux下使用ps命令来查看Oracle相关的进程
  10. 手握14万,博越是最佳选?这车配钢炮动力,可双屏互动,不香?
  11. Sharepoint 2010 备份与恢复 (二)
  12. 用 python 画散点图与气泡图
  13. 代谢组学常见数据库列表和简介
  14. 做公文流转的具体思路
  15. ttest求pvalue_.net 调用R语言的函数(计算统计值pvalue 对应excel :ttest)
  16. (转)s3c6410 开发板Linux系统支持 K9GAG08U0E的方法
  17. 毕业论文页眉页脚页码插入
  18. Bugku CRYPTO No Ciphertext RSA writeup by 树木
  19. java框体怎么添加背景图,文本框添加背景图片,文本框背景图片,public class
  20. Unity3D性能优化——工具篇

热门文章

  1. JAVA时间操作类常用方法汇总
  2. [deviceone开发]-QQ分享、微信分享和新浪微博分享
  3. iTunes Connect(一) —— iOS应用上架到AppStore
  4. Using SQLite database in your Windows 10 apps
  5. E72上安装fring使用skypeout拨打电话
  6. 关于centos7下/etc/sysconfig/目录没有iptables问题
  7. spring中的context:include-filter和context:exclude-filter的区别
  8. Lost and Found(结对项目)功能实现
  9. 关于数据库#1063 - Incorrect column specifier for column 'xxx'异常
  10. PHP的静态变量和引用函数