In this section, you will learn about jQuery selectors & how khổng lồ find DOM element(s) using selectors.
The jQuery selector enables you lớn find DOM elements in your web page. Most of the times you will start with selector function $() in the jQuery.
Bạn sẽ xem: Using jquery khổng lồ find a class by id nameThe selector expression parameter specifies a pattern to lớn match the elements. The jQuery uses CSS selector patterns as well as its own pattern khổng lồ match the elements.
The context parameter is optional. It specifies elements in a DOM hierarchy from where jQuery starts searching for matching elements.
Let"s see commonly used selectors in jQuery.
Select Elements by Name
The most common selector pattern is element name. Specifing an element name as string e.g. $("p") will return an array of all the elements in a webpage.
The following figure shows which DOM elements will be returned from $("p") và $"(div").
Bạn đang xem: Selector
jQuery Selectors kiểm tra
jQuery append() method inserts text at the over in the element. You can get a particular element by using id selector pattern. Specify an id of an element for which you want lớn get the reference, starting with # symbol.
The following figure shows which DOM elements will be returned from $("#myDiv1") và $"(#prg2").
jQuery Id Selector thử nghiệm
Select Elements by AttributejQuery also allows you khổng lồ find an element based on attributes phối on it. Specifing an attribute name in square brackets in $ function e.g. $("") will return all the elements that have class attribute irrespective of value.
In the following example, jQuery returns all the elements that have class or contenteditable attribute irrespective of any value.
jQuery Attribute Selector You can also specify a specific value of an attribute in attribute selector. For example, $("") will return all the elements which have class attribute with myCls as a value.
Bài viết coi nhiều