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

js determines whether an element exists on the page_javascript skills

WBOY
Release: 2016-05-16 17:45:12
Original
1395 people have browsed it
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");
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template