Home > Web Front-end > JS Tutorial > body text

Code to determine whether an element exists under javascript_javascript skills

WBOY
Release: 2016-05-16 18:32:58
Original
994 people have browsed it
1. Determine whether the form element exists (1)
Copy code The code is as follows:

if("periodPerMonth" in document.theForm)
{
return true;
}
else{
return false;
}

2. Determine whether the page element exists
Copy the 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
Copy code code As follows:

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

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

if(document.theForm)
{
//Exists
}

5. Write a script with Jquery
Copy 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