Home Web Front-end JS Tutorial Summary of commonly used jQuery selectors_jquery

Summary of commonly used jQuery selectors_jquery

May 16, 2016 pm 04:42 PM

In Dom programming, we can only use limited functions to obtain Dom objects based on id or TagName. However, it is completely different in jQuery. jQuery provides extremely powerful selectors to help us obtain objects on the page and convert the objects Returned as a jQuery wrapper set. This article mainly introduces and categorizes commonly used jQuery selectors.

jQuery selectors can be roughly divided into 4 categories: basic selectors, hierarchical selectors, filter selectors, and form selectors.
The filter selectors can be divided into: simple filter selectors, content filter selectors, visibility filter selectors, attribute filter selectors, child element filter selectors, and form object attribute filter selectors.

Basic selector:

$("#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 you get 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[/code]
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 child elements with the id value of main
$("label input") Select the next input element node of all label elements
The tested selector returns all input tag elements
that are directly followed by an input tag after the label tag. $("#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
$ ("TD: LL (4)") Select all TD elements with a serial number of less than 4 in the TD element
$(":header")
$("div:animated")

Content filter selector:

$("div:contains('John')") Select all div elements that contain John text

$("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']") Select 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 the 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 the selected elements among all select child elements

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 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.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Can PowerPoint run JavaScript? Can PowerPoint run JavaScript? Apr 01, 2025 pm 05:17 PM

JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.

See all articles