1. Determine whether the object exists
If the following jQuery code is used to determine whether an object exists, it cannot be used
if($("#id")){
}else{}
Because $(“#id”) will return object regardless of whether the object exists.
To correctly determine whether an object exists, you should use:
if($("#id").length>0){}else{}
Use the length property of the jQuery object to determine, if > 0 exists.
or
if($(" #id")[0]){} else {}
Or directly use the native Javascript code to judge:
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
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 span>
three
jQuery code and functions
Jquery.ready ({
alert($(“#ch”).children(“ #sp”).html());
});
$(“#ch”).children() gets the object [twothree ].
$("#ch").children("#sp") filters to get [three ]