本文摘自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怎么删除html元素
jQuery如何验证元素是否为空
jQuery怎么判断复选框是否被选中
jQuery如何处理ajax缓存问题
jQuery怎么使字体变色
jQuery $.post方法不执行怎么办
jQuery click()和onclick()区别是什么
jQuery中#是什么意思
jQuery表示什么?
利用jQuery实现图片放大镜
更多相关阅读请进入《jQuery》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » jquery怎么判断节点是否存在?