


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.

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











There are five basic CSS selectors: 1. Element selector, which locates specific HTML elements through the element name of the HTML page; 2. Class selector, which locates specific HTML elements through the value of the class attribute of the HTML element; 3. ID selection The selector locates specific HTML elements through the value of the id attribute of the HTML element; 4. The wildcard selector "*" can refer to all types of tag elements, including custom elements; 5. The attribute selector selects the HTML elements that have been There are attribute names or attribute values to locate specific HTML elements.

Not included. CSS selectors include: 1. Tag selector, which locates specific HTML elements through the element name of the HTML page; 2. Class selector, which locates specific HTML elements through the value of the class attribute of the HTML element; 3. ID selector, which Locate specific HTML elements through the value of the id attribute of the HTML element; 4. The wildcard selector "*" can refer to all types of tag elements, including custom elements; 5. The attribute selector uses the existing attribute name of the HTML element or attribute value to locate a specific HTML element.

There are many types of CSS3 selectors, which can select elements based on different element properties, structural relationships, or states. The following will introduce several commonly used CSS3 selector types and provide specific code examples. Basic selector: Element selector: Use the element name as the selector, here is the p element as an example: p{color:red;} Class selector: Use the class name as the selector, starting with ., here the class is example Element as an example: .example{fo

js hierarchical selectors include getElementById, getElementsByTagName, getElementsByClassName, querySelector, querySelectorAll, parentNode, children, previousSibling and nextSibling, etc. Detailed introduction: 1. getElementById, use the ID attribute of the element to select, etc.

HTML5 hierarchical selectors include descendant selectors, child element selectors, adjacent sibling selectors, and universal sibling selectors. Detailed introduction: 1. The descendant selector is used to select the descendant elements of an element. It uses spaces to indicate the relationship between elements. 2. The child element selector is used to select the direct child elements of an element. It uses the greater than sign to indicate it. The relationship between elements; 3. The adjacent sibling selector is used to select the next adjacent sibling element of an element. It uses a plus sign to indicate the relationship between elements; 4. The universal sibling selector is used to select the next adjacent sibling element of an element. etc.

The hierarchical selector is a commonly used selector in CSS, which can select based on the relationship between elements. The following are several commonly used hierarchical selectors and code examples: Descendant Selector: Descendant Selector is used to select all descendant elements within a specified element. The syntax is: ancestordescendant. Example: /*Select all elements within

The basic selectors of the code include "getElementById", "getElementsByClassName", "getElementsByTagName", "querySelector" and "querySelectorAll": 1. getElementById, selects elements through the ID attribute of the element, and returns the first matching element; 2. getElementsByClassName, through The class name of the element selects the element and so on.

Hierarchical selectors include descendant selectors, child element selectors, adjacent sibling selectors, universal sibling selectors, adjacent selectors, etc. Detailed introduction: 1. The descendant selector matches the target element by selecting the descendant elements of the element. It uses spaces to indicate the relationship between elements; 2. The child element selector matches the target element by selecting the direct child elements of the element. It uses The ">" symbol represents the relationship between elements; 3. The adjacent sibling selector matches the target element by selecting the next sibling element of the element. It uses the "+" symbol to represent the relationship between elements, etc.
