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 jQuery object Use the length attribute to determine if > 0, it exists.
or
if($("#id")[0]){} else {}
or directly use native Javascript code to judge:
if(document.getElementById("id")){} else {}
2. Find child nodes according to the parent node
jQuery's children() returns the matching object The byte point
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"
3. Find the parent node according to the child node
two
three
jQuery code and functions
Jquery .ready ({
alert($(“#ch”).children(“#sp”).html());
});
$(“#ch”).children() Get the object [
twothree ].
$(“#ch”).children(“#sp”) filter to get [
three ]