js判断dom节点是否存在
宋雪维
2021-03-08 09:46:275413浏览 · 0收藏 · 0评论

本文教程操作环境: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学习推荐:JavaScript教程。
关注公众号,随时随地在线学习