Jquery determines whether an ID exists

Source: Internet
Author: User

Search and processing in jquery

1. Determine whether the object has a http://s.click.taobao.com/t_8? E = 7hz5x % 2bozdswsvvyc5jts79au1q % 3d % 3d & P = mm_24156262_0_0

If the following jqueryCodeIt is unavailable to determine whether an object exists.

 
If ($ ("# ID ")){
} Else {}

Because $ ("# ID") returns an object regardless of whether the object exists.

Correct use:

 
If ($ ("# ID"). length> 0) {} else {}

Use the Length attribute of the jquery object to determine whether it exists if it is greater than 0.

Or

If ($ ("# ID") [0]) {} else {}

Or you can directly use the native JavaScript code to determine:

If (document. getelementbyid ("ID") {} else {}

2. Search for Child Nodes Based on the parent node
Jquery children () returns the byte point of the matched object
Children () returns the Child point of the matched object.
<P> one </P>
<Div id = "ch">
<Span> two </span>
</Div>
Jquery code and functions:
Function JQ (){
Alert({{ch}}.children({.html ());
}
$ ("# Ch"). Children (). The result of the object [<span> two </span>. .html () is "two"

  3. Find the parent node based on the child node 


two
three


jquery code and functions
jquery. ready ({
alert(({{ch}}.children(??sp=}.html ();
});
$ ("# Ch "). obtain the object [ two three ] From children ().
$ ("# Ch "). [ three ]

http://s.click.t Aobao.com/t_8? E = 7hz5x % 2bozdswsvvyc5jts79au1q % 3d % 3d & P = mm_24156262_0_0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.