本文教程操作环境:windows7系统、jquery3.2.1版本,DELL G3电脑。
方法一:判断length是否大于等于1
length大于等于1,则dom节点存在
Object.prototype.exist = function(){ if(typeof this !='undefined' && this.length>=1){ return true; } return false; };
方法二:使用document.getElementById方法判断
返回null则dom节点不存在
if (document.getElementById("map")) { console.log("存在"); console.log(document.getElementById("map")); } else { console.log("不存在"); console.log(document.getElementById("map")); }
以上就是小编整理总结的JavaScript中判断dom是否存在节点的两种方法,大家都可以尝试看看哦·更多JavaScript学习推荐:。
Copyright © 2019- net188.cn 版权所有 湘ICP备2022005869号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务