jQuery selectors are powerful tools that every developer uses in daily life. They can accurately select the required elements from the DOM. This list brings together 50 commonly used jQuery selectors for reference by all jQuery developers. Note that these selectors are not arranged in a specific order.
$(“*”)
– Select all elements in the document. $(“p > *”)
– Select the child elements of all paragraph elements. $(“#specialID”)
– Select the element with ID "specialID". $(“.specialClass”)
– Select all elements with class as "specialClass". $(“li:not(.myclass)”)
– Select all elements that match the li
selector, but exclude elements whose class is "myclass". $(“a#specialID.specialClass”)
– Select the link element with ID "specialID" and class "specialClass". $(“p a.specialClass”)
– Select the link element within the paragraph element with class "specialClass". $(“ul li:first”)
– Select the first list item element of the unordered list. $(“#container p”)
– Select all descendant paragraph elements with ID "container" element. $(“li > ul”)
– Select the child unordered list element of all list item elements. $(“strong em”)
– Select the italic element that follows the bold element. $(“p ~ ul”)
– Select all unordered list elements that follow the paragraph element. $(“code, em, strong”)
– Select the code, italic, or bold element. $(“p strong, .myclass”)
– Select the bold elements within the paragraph element, and all elements whose class is "myclass". $(“:empty”)
– Select all elements without children. $(“p:empty”)
– Select all paragraph elements without child elements. $(“div[p]”)
– Select the div element that contains the paragraph elements. $(“p[.myclass]”)
– Select the paragraph element that contains the "myclass" element. $(“a[@rel]”)
– Select all link elements with rel attribute. $(“input[@name=myname]”)
– Select the input element whose name attribute value is exactly equal to "myname". $(“input[@name^=myname]”)
– Select the input element whose name attribute value begins with "myname". $(“a[@rel$=self]”)
– Select the link element with the value of the rel attribute ending with "self". $(“a[@href*=domain.com]”)
– Select the link element with the href attribute value that contains "domain.com". $(“li:even”)
– Select the list item element with an index value of even. $(“tr:odd”)
– Select the table row element with an index value of odd numbers. $(“li:first”)
– Select the first list item element. $(“li:last”)
– Select the last list item element. $(“li:visible”)
– Select all visible list item elements. $(“li:hidden”)
– Select all hidden list item elements.$(“:radio”)
– Select all radio buttons in the form. $(“:checked”)
– Select all selected check boxes in the form. $(“:input”)
– Select form elements (input, select, textarea, button). $(“:text”)
– Select the text input element (input[type=text]
). $(“li:eq(2)”)
– Select the third list item element. $(“li:eq(4)”)
– Select the fifth list item element. $(“li:lt(2)”)
– Select the list item element (first two) before the third element. $(“p:lt(3)”)
– Select the paragraph element before the fourth element (first three). $(“li:gt(1)”)
– Select the list item element after the second element. $(“p:gt(2)”)
– Select the paragraph element after the third element. $(“div/p”)
– Select the subparagraph element of the div element. $(“div//code”)
– Select all descendant code elements of the div element. $(“//p//a”)
– Select descendant link elements for all paragraph elements. $(“li:first-child”)
– Select all list item elements that are the first child element of its parent element. $(“li:last-child”)
– Select all list item elements that are the last child element of its parent element. $(“:parent”)
– Select all elements that have at least one child element (including text). $(“li:contains(second)”)
– Select the list item element that contains the text "second". $(“td:gt(4)”)
– Select the fifth and later table cell element. $(“input:not(:checked)”)
– Select all input elements that are not selected. $(“div,span,p.myClass”)
– Select an element that matches any of the three selectors. $(“input[id][name$=”man”]”)
– Select an input element that has both the id attribute and the name attribute ends with "man". jQuery Selector FAQs (FAQs)
attribute equals the selector to select an element whose attribute value exactly matches a specific value, such as selecting an element with a specific ID or class. The attribute contains selector is used to select the element that contains the specified substring, even if the value is only part of the attribute value.
Use attribute equals selector: $(“element[name=’value’]”)
. For example, select the input element named "username": $(“input[name=’username’]”)
.
Attribute Start Selector is used to select an element whose attribute value begins with a specified string: $(“element[attribute^=’value’]”)
. For example, select all elements whose ID starts with "my": $(“[id^=’my’]”)
.
Separate selectors with commas: $(“div, p”)
Select all div and p elements.
Use :first-child
Selector: $(“element:first-child”)
.
Use :last-child
Selector: $(“element:last-child”)
.
Use :even
Selector: $(“element:even”)
.
Use :odd
Selector: $(“element:odd”)
.
Use :contains()
Selector: $(“element:contains(‘text’)”)
.
Use attribute selector: $(“element[attribute]”)
.
I hope this more comprehensive guide will be helpful to you!
The above is the detailed content of Top 50 jQuery Selectors. For more information, please follow other related articles on the PHP Chinese website!