Home > Web Front-end > JS Tutorial > Code to determine whether Id exists under Jquery_jquery

Code to determine whether Id exists under Jquery_jquery

WBOY
Release: 2016-05-16 18:12:42
Original
1448 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

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 ]
Related labels:
id
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