In order to describe the key points 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 commonly used ones will be implemented here.
1, obtained by id, this element is unique
$('#id')
2, obtained by className
$('.cls') Gets all elements in the document whose className is cls
$('.cls', el)
$('.cls', '#id')
$('span.cls') Gets all span elements with className cls in the document
$('span.cls', el) Gets the elements with className cls in the specified element, el is HTMLElement (not recommended)
$('span.cls', '#id') Get the element whose className is cls among the elements with the specified id
3, get
$(' through tagName span') Get all span elements in the document
$('span', el) Get the span element in the specified element, el is HTMLElement (not recommended)
$('span', '#id') Get the span element in the element with the specified id
4, get
$('[name]') through attribute Get the element with attribute name in the document
$( '[name]', el)
$('[name]', '#id')
$('[name=uname]') Get all elements with attribute name=uname in the document
$('[name=uname]', el)
$('[name=uname]', '#id')
$('input[name=uname]') Get all attribute names in the document =uname's input element
$('input[name=uname]', el)
$('input[name=uname]', '#id')
Example:
ccc
Firebug output is as follows