search
HomeWeb Front-endJS TutorialComprehensive summary of jQuery selectors_jquery

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
    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
    From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

    The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

    JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

    Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

    Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

    JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

    Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

    I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

    How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

    This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

    JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

    JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

    The Evolution of JavaScript: Current Trends and Future ProspectsThe Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AM

    The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

    Demystifying JavaScript: What It Does and Why It MattersDemystifying JavaScript: What It Does and Why It MattersApr 09, 2025 am 12:07 AM

    JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

    See all articles

    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)
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    SublimeText3 Linux new version

    SublimeText3 Linux new version

    SublimeText3 Linux latest version

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft