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