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

An example tutorial on determining whether an object exists in jQuery

零下一度
Release: 2017-06-17 15:26:28
Original
1064 people have browsed it

If the following jquery code is used to determine whether an object exists, it cannot be used.

if($("#id")){
    //...
}else{
    //...
}
Copy after login

Because $("#id") will return object regardless of whether the object exists.

Correct useTo determine whether the object existsYou should use:

if($("#id").length>0){
    //...
}else{
    //...
}
Copy after login

Use the jQuery object's property length to determine if > 0 it exists.

or

if($("#id")[0]){
    //...
}else{
    //...
}
Copy after login

Or directly use the native Javascript code to judge:

if(document.getElementById("id")){    //...}else{    //...}
Copy after login

The above is the detailed content of An example tutorial on determining whether an object exists in jQuery. For more information, please follow other related articles on the PHP Chinese website!

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