Home Web Front-end JS Tutorial Comprehensive summary of jQuery selectors_jquery

Comprehensive summary of jQuery selectors_jquery

May 16, 2016 pm 05:05 PM
jquery Selector

jQuery’s selector is extremely powerful. Here is a brief summary of commonly used element search methods

jQuery selectors make obtaining page elements easier and more flexible, greatly reducing developer stress. Just like building a building, without bricks and tiles, you cannot build a building. How can we talk about other operations if we can't get elements? It can be seen that the importance of jQuery selector. Of course, it is very difficult to master all selectors at once. This requires practice and accumulation.

Now we officially enter the study of jQuery selector. We classify and learn jQuery selectors and divide jQuery selectors into the following types:

1. Basic selector

◦id Select
based on element ID ◦elementname Select
based on element name ◦classname Select

based on the element css class name

Example:

Copy code The code is as follows:

Copy code The code is as follows:

jQuery("#ID").val();
jQuery("a").text();
jQuery(".classname").val();

You can get the value of the elements respectively. The above three are the most common selectors, among which the ID selector is the most efficient and should be used whenever possible.

2. Level selector

◦ancestor descendant Ancestor and descendant selector
◦parent > child Parent-child node selector
◦prev next Same level selector
◦prev ~ siblings Filter selector

Example:

Copy code The code is as follows:






1
2

Copy code The code is as follows:

//Get the content of the a tag in the div. The result is 12
jQuery("#divTest a").text();
//Output the direct child node of the div and the result is investment
jQuery("#divTest>input").val();
//Output the next element of the same level with the id next and the result is responsible
jQuery("#next input").val();
//Same as above, and it is an element with a title. The result is learning
jQuery("#next~[title]").val();

3. Basic filter selector

◦:first Find the first element
◦:last Find the last element
◦:not(selector) Remove elements matching the given selector
◦:even Matches elements with even index values, counting from 0
◦:odd Matches elements with odd index values ​​Counting from 0
◦:eq(index) Matches an element with a given index starting from 0
◦:gt(index) Matches elements
greater than the given index value ◦:lt(index) Matches elements
that are less than the given index value ◦:header Select tags such as h1, h2, h3 (not used yet)
◦:animated Matches elements that are performing animation effects (not used yet)

Example:

Copy code The code is as follows:



                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
              






Copy code

The code is as follows: //The first li content results in investment jQuery("li:first").text();
//The last li content results in responsibility
jQuery("li:last").text();
//Input the unselected value and the result is not learning
jQuery("li input:not(:checked)").val();
//The result of li with an even number is investment mature
jQuery("li:even").text();
//The result of li whose index is an odd number is the financial management responsibility
jQuery("li:odd").text();
//The content of li with index greater than 2 results in responsibility
jQuery("li:gt(2)").text();
//The content of li with index less than 1 results in investment
jQuery("li:lt(1)").text();




4. Content filter

◦:contains(text) Matches elements that contain the given text ◦:empty Matches all empty elements that do not contain child elements or text ◦:has(selector) Matches the elements matched by the selector

Example:

Copy code

The code is as follows:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               


    Copy code The code is as follows:

    //The content of li containing hyip results in hyip investment hyip
    jQuery("li:contains('hyip')").text();
    //The content of the next li of the empty li results in financial management
    jQuery("li:empty li").text();
    //The content of li containing the a tag results in investment
    jQuery("li:has(a)").text();

    5. Visibility filter

    ◦:hidden Matches invisible elements
    ◦:visible Matches visible elements

    Example:

    Copy code The code is as follows:


    • Visible

    • Invisible


    Copy code The code is as follows:

    //The content of invisible li results in invisible
    jQuery("li:hidden").text();
    //The content of visible li is visible
    jQuery("li:visible").text();

    6. Attribute filter

    ◦[attribute=value] Matches elements whose attribute is the given value
    ◦[attribute^=value] The matching attribute is an element starting with the given value
    ◦[attribute$=value] The matching attribute is an element that ends with the given value
    ◦[attribute*=value] Matches elements whose attribute contains the given value

    Example:

    Copy code The code is as follows:




    //name is the value of hyipinvest and the result is hyipinvest
    alert(jQuery("input[name='hyipinvest']").val());
    //The value whose name starts with hyip results in hyip investment
    alert(jQuery("input[name^='hyip']").val());
    //The value whose name ends with hyip results in investment hyip
    alert(jQuery("input[name$='hyip']").val());
    //name contains the value of oo. The result is HYIP
    alert(jQuery("input[name*='oo']").val());

    JQuery selector is summarized here. These are basically encountered in the learning process, and there are still a few parts that have not been summarized. After a period of practice, I believe everyone will be able to use the jQuery selector skillfully.

    $("#myELement") Select the element whose id value is equal to myElement. The id value cannot be repeated. There can only be one id value myElement in the document, so what you get is the only element
    $("div") Select all div tag elements and return an array of div elements
    $(".myClass") Select all elements using css of myClass class
    $("*") Select all elements in the document. You can use a variety of selection methods for joint selection: For example, $("#myELement,div,.myclass")

    Cascading selector:
    $("form input") Select all input elements in form elements
    $("#main > *") Select all sub-elements whose id value is main
    $("label input") Selects the next input element node of all label elements. After testing, the selector returns all input label elements directly followed by an input label
    $("#prev ~ div") Sibling selector, this selector returns all div tags belonging to the same parent element of the tag element with id prev

    Basic filter selector:
    $("tr:first") Select the first of all tr ​​elements
    $("tr:last") Select the last of all tr ​​elements
    $("input:not(:checked) span")

    Filter out: all input elements of the checked selector

    $ ("TR: EVEN") Select the 0, 2, 4 ... ... Personal element of all TR elements (Note: Because the multiple elements selected are the array when they choose, so the serial number starts from 0)
    $("tr:odd") Select the 1st, 3rd, 5th... elements of all tr ​​elements
    $("td:eq(2)") Select the td element with serial number 2 among all td elements
    $("td:gt(4)") Select all td elements with sequence numbers greater than 4 in td elements
    $("td:ll(4)") Select all td elements with sequence numbers less than 4 in the td elements
    $(":header")
    $("div:animated")

    Content filter selector:


    $("div:contains('John')") selects all elements containing John text in divs
    $("td:empty") Selects an array of all td elements that are empty (not including text nodes)
    $("div:has(p)") Select all div elements containing p tags
    $("td:parent") Select all element arrays with td as the parent node

    Visual filter selector:


    $("div:hidden") Select all hidden div elements
    $("div:visible") Select all visible div elements

    Attribute filter selector:

    $("div[id]") Select all div elements containing the id attribute
    $("input[name='newsletter']") Select all input elements whose name attribute is equal to 'newsletter'

    $("input[name!='newsletter']") selects all input elements whose name attribute is not equal to 'newsletter'

    $("input[name^='news']") Select all input elements whose name attribute starts with 'news'
    $("input[name$='news']") Select all input elements whose name attribute ends with 'news'
    $("input[name*='man']") Select all input elements whose name attribute contains 'news'

    $("input[id][name$='man']") You can use multiple attributes for joint selection. This selector gets all the elements that contain the id attribute and the attribute ends with man

    Child element filter selector:

    $("ul li:nth-child(2)"),$("ul li:nth-child(odd)"),$("ul li:nth-child(3n 1)")

    $("div span:first-child") Returns an array of the first child nodes of all div elements
    $("div span:last-child") Returns an array of the last node of all div elements
    $("div button:only-child") Returns an array of all child nodes that have only one child node in all divs

    Form element selector:

    $(":input") Select all form input elements, including input, textarea, select and button

    $(":text") Select all text input elements
    $(":password") Select all password input elements
    $(":radio") Select all radio input elements
    $(":checkbox") Select all checkbox input elements
    $(":submit") Select all submit input elements
    $(":image") Select all image input elements
    $(":reset") Select all reset input elements
    $(":button") Select all button input elements
    $(":file") Select all file input elements
    $(":hidden") Select all input elements or hidden fields of the form that are of type hidden

    Form element filter selector:

    $(":enabled") Select all operable form elements
    $(":disabled") Select all inoperable form elements
    $(":checked") Select all checked form elements
    $("select option:selected") selects all selected elements among the child elements of select


    Select the text value of the previous td of the input text box named "S_03_22"
    $("input[@ name =S_03_22]").parent().prev().text()

    Name starts with "S_" and does not end with "_R"
    $("input[@ name ^='S_']").not("[@ name $='_R']")

    The value selected by a radio named radio_01
    $("input[@ name =radio_01][@checked]").val();


    $("A B") finds all child nodes under the A element, including indirect child nodes
    $("A>B") finds the direct child nodes under the A element
    $("A B") finds the sibling nodes behind the A element, including indirect child nodes
    $("A~B") finds the sibling nodes behind the A element, excluding indirect child nodes

    1. $("A B") finds all child nodes under the A element, including indirect child nodes

    Example: Find all input elements in the form

    HTML code:









    jQuery code:
    $("form input")

    Result:
    [ , ]

    2. $("A>B") finds the direct child nodes under the A element

    Example: Match all child input elements in the form.

    HTML code:









    jQuery code:
    $("form > input")

    Result:
    [ ]

    3. $("A B") finds the sibling nodes behind the A element, including indirect child nodes

    Example: Match all input elements following label

    HTML code:









    jQuery code:
    $("label input")

    Result:
    [ , ]

    4. $("A~B") finds the sibling nodes behind the A element, excluding indirect child nodes

    Example: Find all input elements that are siblings of the form

    HTML code:









    jQuery code:
    $("form ~ input")

    Result:
    [ ]

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

    Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

    How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

    How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

    How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

    How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

    jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

    Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

    Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

    Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

    How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

    How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

    Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

    jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

    Introduction to how to add new rows to a table using jQuery Introduction to how to add new rows to a table using jQuery Feb 29, 2024 am 08:12 AM

    jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:

    See all articles