如何查找具有特定id的元素是否存在 [英] How to find if element with specific id exists or not

查看:184
本文介绍了如何查找具有特定id的元素是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的JavaScript中,我想检查具有特定id的元素是否存在,我尝试了2种方式

In my JavaScript I want to check whether the element with specific id is exist or not, I tried it with 2 ways

1)。

var myEle = document.getElementById("myElement");
if(myEle  == null){
   var myEleValue= document.getElementById("myElement").value;
}

2)。

if(getElementById("myElement")){
    var myEleValue= document.getElementById("myElement").value;
}

但它给出了同样的错误 -

but it gives same error as below -


预期对象

Object expected


推荐答案

 var myEle = document.getElementById("myElement");
    if(myEle){
        var myEleValue= myEle.value;
    }

这篇关于如何查找具有特定id的元素是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆