


Detailed 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、表单选择器
由于过滤选择器内容比较多,因此本文仅介绍前两种,下篇文章将介绍后两种。
一点准备工作
为了能让大家看到具体的效果,这里先创建一个示例页面,里面包含各种
示例页面代码:
id为one,class为one的div
id为two,class为one,title为test的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 的
|
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的元素 后面所有 兄弟元素
|
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.

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

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 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 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.

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.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.

How to send task notifications in Quartz In advance When using the Quartz timer to schedule a task, the execution time of the task is set by the cron expression. Now...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment