本文摘自PHP中文网,作者青灯夜游,侵删。
jquery判断节点是否存在的方法:首先使用length属性获取节点对象中元素的数目;然后判断获取的元素数目是否大于0,如果大于0则节点存在,否则不存在;语法格式“if ($(selector).length>0) {//存在}”。

相关推荐:《jQuery视频》
jquery 判断节点是否存在
jquery
1 2 3 4 5 | if ($( "#map" ).length > 0) {
console.log( "true" );
} else {
console.log( "false" );
}
|
补:js
1 2 3 4 5 6 7 | if (document.getElementById( "map" )) {
console.log( "存在" );
console.log(document.getElementById( "map" ));
} else {
console.log( "不存在" );
console.log(document.getElementById( "map" ));
}
|


1 2 3 4 5 | if (!!document.getElementById( "map" )) {
console.log( "true" );
} else {
console.log( "false" );
}
|
更多编程相关知识,请访问:编程学习课程!!
以上就是jquery怎么判断节点是否存在?的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
jQuery 怎么写if
jQuery 判断是否汉字的方法
jQuery中$符号的作用是什么?
jQuery的选择器有几种
jQuery怎么去除字符串的空格?
jQuery如何获取同级元素
jQuery怎么判断dom节点是否存在
jQuery怎么样判断元素是否隐藏起来
两分钟了解jQuery与javascript、js 三者间的区别
jQuery自定义函数应用以及解析
更多相关阅读请进入《jQuery》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » jquery怎么判断节点是否存在?