search
HomeWeb Front-endJS TutorialDetailed explanation of the powerful jQuery selector: basic selector and hierarchical selector_jquery

jQuery允许开发者使用从CSS1到CSS3几乎所有的选择器,以及jQuery独创的高级而复杂的选择器。另外还可以加入插件使其支持XPath选择器,甚至开发者可以编写属于自己的选择器(即选择器插件,参考上篇:jQuery插件原来如此简单——jQuery插件的机制及实战)。正是jQuery强大的选择器功能,让它很容易上手,吸引了大批的开发者,本文就来介绍一下强大的jQuery选择器。
jQuery选择器类型
  jQuery选择器主要分为四类:
  1、基本选择器
  2、层次选择器
  3、过滤选择器
  4、表单选择器
  由于过滤选择器内容比较多,因此本文仅介绍前两种,下篇文章将介绍后两种。
一点准备工作
  为了能让大家看到具体的效果,这里先创建一个示例页面,里面包含各种

元素,然后用jQuery选择器来匹配元素并调整它们的样式。
  示例页面代码:
复制代码 代码如下:




Demo





id为one,class为one的div
class为mini



id为two,class为one,title为test的div
class为mini,title为other

class为mini,title为test



class为mini

class为mini

class为mini




class为mini

class为mini

class为mini

class为mini,title为tesst



class为hide的div


包含input的type为hidden的div


正在执行动画的span元素



基本选择器
  基本选择器是jQuery中最常用的选择器,也是最简单的选择器,它通过元素id、class和标签名等来查找DOM元素。在网页中,每个id名称只能用一次,class允许重复使用。

  基本选择器规则如下:

基本选择器
选 择 器 描 述 返 回 示 例
#id 根据给定的id匹配一个元素 单个元素 $("#test")选取 id 为 test 的元素
.class 根据给定的类名匹配元素 集合元素 $(".test")选取所有 class 为 test 的元素
element 根据给定的元素名匹配元素 集合元素 $("p")选取所有的

元素

* 匹配所有元素 集合元素 $("*")选取所有的元素
selector1,selector2,
...,selectorN
将每一个选择器匹配到的元
素合并后一起返回
集合元素 $("div,span,p.myClass")选取所有

和拥有class为 myClass 的


标签的一组元素

Online demonstrationhttp://demo.jb51.net/js/2012/jquery_demo/jQuery basic selector example.html
Hierarchical selector
If you want to pass the DOM To obtain specific elements based on the hierarchical relationship between elements, such as descendant elements, child elements, adjacent elements, sibling elements, etc., then the hierarchical selector is a very good choice.

The rules for hierarchical selectors are as follows:

Hierarchical selector
选 择 器 描 述 返 回 示 例
$("ancestor descendant")

选取ancestor元素里所有

descendant(后代)元素

集合元素

$("div span")选取

里的所

有的元素

$("parent>child") 选取parent元素下的child(子)元素 集合元素

$("div>span")选取

元素下

元素名是的子元素

$("prev next") 选取紧接在prev元素后的next元素 集合元素

$(".one div")选取class为one的

下一个

兄弟元素
$("prev~siblings") 选取prev元素之后的所有siblings元素 集合元素

$("#two~div")选取id为two的元素

后面所有

兄弟元素
Online demohttp://demo.jb51.net/js/2012/jquery_demo/jQuery hierarchical selector example.html
In the hierarchical selector, the 1st and 2nd They are more commonly used, and the latter two are relatively less likely to be used because they can be replaced by simpler methods in jQuery.

You can use the next() method to replace the $("prev next") selector, that is, $(".one div") is equivalent to $(".one").next("div") .

You can use the nextAll() method to replace the $("prev~siblings") selector, that is, $(".one~div") and $(".one").nextAll("div") Equivalent.

Summary
This article mainly introduces the basic selectors and hierarchical selectors in jQuery selectors, and gives sample code for each type of selector. I hope it will be helpful to everyone. I am also a beginner of jQuery, so everyone is welcome to try it out.
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
Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

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

Video Face Swap

Video Face Swap

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

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment