本文摘自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对象是input还是select
jQuery url中传递中文乱码怎么办
jQuery如何使用新元素来替换所选元素
ajax与jQuery的区别是什么
live jQuery 不支持吗
jQuery ui和easyui的区别是什么?
jQuery选择器优点有什么
jQuery 怎么判断字符串是否包含特定字符
jQuery 2.x和1.x的区别是什么
jQuery如何使用正则
更多相关阅读请进入《jQuery》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » jquery怎么判断节点是否存在?