Javascript 如何查找具有特定 id 的元素是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42526032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find if element with specific id exists or not
提问by Amita Patil
In my JavaScript I want to check whether the element with specific id is exist or not, I tried it with 2 ways
在我的 JavaScript 中,我想检查具有特定 id 的元素是否存在,我尝试了两种方法
1).
1)。
var myEle = document.getElementById("myElement");
if(myEle == null){
var myEleValue= document.getElementById("myElement").value;
}
2).
2)。
if(getElementById("myElement")){
var myEleValue= document.getElementById("myElement").value;
}
but it gives same error as below -
但它给出了与下面相同的错误 -
Object expected
预期对象
回答by Mr.Bruno
var myEle = document.getElementById("myElement");
if(myEle){
var myEleValue= myEle.value;
}
回答by Weedoze
You can simply use if(yourElement)
你可以简单地使用 if(yourElement)
var a = document.getElementById("elemA");
var b = document.getElementById("elemB");
if(a)
console.log("elemA exists");
else
console.log("elemA does not exist");
if(b)
console.log("elemB exists");
else
console.log("elemB does not exist");
<div id="elemA"></div>
回答by Armin
You need to specify which object you're calling getElementById from. In this case you can use document. You also can't just call .value on any element directly. For example if the element is textbox .value will return the value, but if it's a div it will not have a value.
您需要指定从哪个对象调用 getElementById。在这种情况下,您可以使用文档。您也不能直接在任何元素上调用 .value 。例如,如果元素是 textbox .value 将返回值,但如果它是一个 div 它将没有值。
You also have a wrong condition, you're checking
你也有一个错误的条件,你正在检查
if (myEle == null)
如果(myEle == null)
which you should change to
你应该改成
if (myEle != null)
如果 (myEle != null)
var myEle = document.getElementById("myElement");
if(myEle != null) {
var myEleValue= myEle.value;
}
回答by khoekman
getElementById
getElementById
Return Value: An Element Object, representing an element with the specified ID. Returns null if no elements with the specified ID exists see: https://www.w3schools.com/jsref/met_document_getelementbyid.asp
返回值: 一个元素对象,表示具有指定 ID 的元素。如果不存在具有指定 ID 的元素,则返回 null,请参见:https: //www.w3schools.com/jsref/met_document_getelementbyid.asp
Truthy vs Falsy
真与假
In JavaScript, a truthy value is a value that is considered true when evaluated in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, "", null, undefined, and NaN). see: https://developer.mozilla.org/en-US/docs/Glossary/Truthy
在 JavaScript 中,真值是在布尔上下文中计算时被视为真值的值。所有值都是真值,除非它们被定义为假(即,假、0、""、空、未定义和 NaN 除外)。请参阅:https: //developer.mozilla.org/en-US/docs/Glossary/Truthy
When the dom element is not found in the documentit will return null. null is a Falsy and can be used as boolean expressionin the if statement.
当在 中找不到 dom 元素时document,它将返回null。null 是一个 Falsy,可以boolean expression在 if 语句中使用。
var myElement = document.getElementById("myElement");
if(myElement){
// Element exists
}
回答by Rahil Lakhani
document.getElementById('yourId')
is the correct way.
是正确的方法。
the document refers the HTML document that is loaded in the DOM.
文档是指加载到 DOM 中的 HTML 文档。
and it searches the id using the function getElementById() which takes a parameter of the id of an element
并使用函数 getElementById() 搜索 id,该函数采用元素 id 的参数
Solution will be :
解决方案将是:
var elem = (document.getElementById('myElement'))? document.getElementById('myElement').value : '';
/* this will assign a value or give you and empty string */
回答by Debbie Kurth
Use typeof for elements checks.
使用 typeof 进行元素检查。
if(typeof(element) === 'undefined')
{
// then field does not exist
}

