Home > Web Front-end > JS Tutorial > Jquery method to determine whether the object obtained by $('#id') exists_jquery

Jquery method to determine whether the object obtained by $('#id') exists_jquery

WBOY
Release: 2016-05-16 17:21:33
Original
1278 people have browsed it

1. Determine whether the object exists

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

Copy code The code is as follows:

if($("#id")){
}else{}

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

To correctly determine whether an object exists, you should use:
Copy the code The code is as follows:

if($("#id").length>0){}else{}

Use the length property of the jQuery object to determine, if > 0 exists.

or
Copy code The code is as follows:

if($(" #id")[0]){} else {}

Or directly use the native Javascript code to judge:
Copy code The code is as follows:

if(document.getElementById("id")){} else {}

2. Find child nodes based on the parent node
jQuery's children() returns the byte point of the matching object
children() returns the child point of the matching object
Copy code The code is as follows:

one



two

jQuery code and functions:
function jq(){
alert($(“#ch”).children(). html());
}
$("#ch").children() gets the object [ two ]. So the result of .html() is "two"
[code]
3. Find the parent node based on the child node
[code]

two
three


jQuery code and functions
Copy code The code is as follows:

Jquery.ready ({
alert($(“#ch”).children(“ #sp”).html());
});
$(“#ch”).children() gets the object [twothree ].
$("#ch").children("#sp") filters to get [three ]
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