Read jQuery 3 (build selector) _ jquery-js tutorial

Source: Internet
Author: User
The previous two articles have explained the core framework of jQuery. I have figured out the composition of jQuery objects and how to use the extend method to extend the library. The chain operation only returns this in the method body. To describe the focus of each article, the sample code is the most streamlined. For example, the selector can only pass HTMLElement and id.

In this article, we will enhance the selector. Based on the 2/8 principle, only the most common ones are implemented here.


1. Obtained by id. This element is unique.
$ ('# Id ')

2. Get it through className
$ ('. Cls') gets all the elements whose className is cls in the document.
$ ('. Cls', el)
$ ('. Cls',' # id ')
$ ('Span. cls') obtains all span elements whose className is cls in the document.
$ ('Span. cls', el) gets the elements whose className is cls and whose el is HTMLElement (not recommended)
$ ('Span. cls', '# id') obtains the elements whose className is cls in the element with the specified id.

3. Get it by tagName
$ ('Span ') get all span elements in the document
$ ('Span ', el) gets the span element in the specified element. el is HTMLElement (not recommended)
$ ('Span ',' # id') gets the span element of the element with the specified id.

4. Get it through attribute
$ ('[Name]') Get the element with the attribute name in the document
$ ('[Name]', el)
$ ('[Name]', '# id ')
$ ('[Name = uname]') Get all the elements with the attribute name = uname in the document
$ ('[Name = uname]', el)
$ ('[Name = uname]', '# id ')
$ ('Input [name = uname] ') Get all the input elements whose attribute name = uname in the document
$ ('Input [name = uname] ', el)
$ ('Input [name = uname] ',' # id ')

Example:

The Code is as follows:






Zchain test

Related Article

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.