js判断dom节点是否存在

406次阅读
没有评论

js判断dom节点是否存在

本文教程操作环境: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教程。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:JavaScript2022-12-16发表,共计560字。
新手QQ群:570568346,欢迎进群讨论 Python51学习