Home  >  Article  >  Web Front-end  >  js determines whether an element exists on the page_javascript skills

js determines whether an element exists on the page_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:45:121371browse
1. Determine whether the form element exists (1)
Copy code The code is as follows:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->if("periodPerMonth" in document.theForm)
{
return true;
}
else{
return false;
}

2. Determine whether the page element exists
Copy code The code is as follows:

if(document.getElementById("XXX"))
{
//Exists
}

3. Determine whether the form element exists (2)
Copy code The code is as follows:

if(document.theForm.periodPerMonth)
{
//Exists
} or
if(typeof(document.theForm.periodPerMonth) == "object")
{
//Exists
}

4. Determine whether the form exists
Copy code The code is as follows:

if(document.theForm)
{
//Exists
}

5. Write a script with Jquery
Copy the code The code is as follows:

if ( $("#someID").length > 0 ) {
$("#someID").text("hi");
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn