search
HomeWeb Front-endJS TutorialJQuery Selector Special Detailed Summary_jquery

这是看《锋利的jquery》时,整理出来的一些东西,很多方法,需要大家亲自实践一下,才会理解得更加深刻,切莫眼高手低哦……

Jquery选择器分类:基本选择器,层次选择器,过滤选择器,表单选择器

一、基本选择器jquery中最常用的选择器,也是最简单的选择器。通过元素idclass和标签名等来查找DOM元素。

-基本选择器

选择器

描述

返回

示例

#id

根据给定的id匹配一个元素

单个元素

$(“#test”)选取idtest的元素

.class

根据给定的类名匹配元素

集合元素

$(“.test”)选取所有classtest的元素

element

根据给定的的元素名匹配元素

集合元素

$(“p”) 选取所有的

元素

*

匹配所有元素

集合元素

$(“*”)选取所有元素

Selector1,Selector2, ……, SelectorN

将每一个选择器匹配到的元素合并后一起返回

集合元素

$(“div,span,p.myClass”)选取所有

,和拥有classmyClass

标签的一组元素

二、层次选择器:通过DOM元素之间的层次关系获取特定元素,如后代元素、子元素、相邻元素、兄弟元素等。层次选择器是一个很好的选择

-层次选择器

选择器

描述

返回

示例

$(“ancestor  descendant”)

选取ancestor元素里所有descendant(后代)元素

集合元素

$(“div span”)选取

里所有的元素

$(“parent>child”)

选取父元素下的子元素

集合元素

$(“div>span”)选取

下元素名为的子元素

$(‘prev+next')

选取紧接在prev元素后的next元素

集合元素

$(‘.one+div') 选取classone的下一个

元素(相邻元素)

$(‘prev~siblings')

选取prev元素之后的所有元素

集合元素

$(‘.one+div') 选取classone的元素后面所有

兄弟元素

 

注意:

$(‘prev+next')选择器与next()方法的等价关系

$(‘.one+div')    等价于     $(“.one”).next(“div”)

$(‘prev~siblings')选择器与nextAll()方法的等价关系

$(‘.one~div')     等价于    $(“.one”).nextAll(“div”)

 

三、过滤选择器:主要是通过特定的过滤选择器规则来筛选出所需的DOM元素,过滤规则与css中伪类选择器语法相同,即选择器都以一个冒号(:)开头。

按照不同的过滤规则,过滤选择器分为:基本过滤选择器、内容过滤选择器、可见性过滤选择器、属性过滤选择器、子元素过滤选择器、表单对象属性座率选择器

 

1-基本过滤选择器

 

选择器

描述

返回

示例

:first

选取第一个元素

单个元素

$(“div:first”)选取所有

元素中第一个
元素

:last

选取最后一个元素

单个元素

$(“div:last”)选取所有

元素中最后一个
元素

:not(selector)

去除所有与给定选择器匹配的元素

集合元素

$(“input:not(.myClass)”) 选取class为不是myClass元素

:even

选取索引是偶数的所有元素,索引从0开始

集合元素

$(“input:even”) 选取索引是偶数的元素

:odd

选取索引是奇数的所有元素,索引从0开始

集合元素

$(“input:odd”) 选取索引是奇数的元素

:eq(index)

选取索引等于index的元素(index0开始)

单个元素

$(“input:eq(1)”选取索引为1元素

:gt(index)

选取索引大于index的元素(index0开始)

集合元素

$(“input:gt(1)”) 选取索引大于1元素(注:大于1,而不包括1

:lt(index)

选取索引小于index的元素(index0开始)

集合元素

$(“input:gt(1)”) 选取索引小于1元素(注:小于1,而不包括1

:header

选取所有的标题元素,例如h1h2h3等待

集合元素

$(“:header”)选取网页中所有的

……

:animated

选取当前正在执行动画的所有元素

集合元素

$(“div:animated”)选取正在执行动画的

元素

 

2-内容过滤选择器

 

选择器

描述

返回

示例

:contains(text)

选取含有文本内容为”text”的元素

集合元素

$(“div:contains(‘')”)选取含有文本“我”的

元素

:empty

选取不包含子元素或者文本的空元素

集合元素

$(“div:empty”)选取不包含子元素(包括文本元素)的

空元素

:has(selector)

选取含有选择器所匹配的元素的元素

集合元素

$(“div:has(p)”) 选取含有

元素的
元素

:parent

选取含有子元素或者文本元素

集合元素

$(“div:parent”) 选取拥有子元素(包括文本元素)的

元素

 

3-可见性过滤选择器

 

选择器

描述

返回

示例

:hidden

选取所有不可见的元素

集合元素

$(“:hidden”)选取所有不可见的元素。包括

等元素。如果只想选取元素,可以使用$(“input:hidden”)

:visible

选取不包含子元素或者文本的空元素

集合元素

$(“div:visible”)选取所有可见的

元素

 

4-属性过滤选择器

 

选择器

描述

返回

示例

[attribute]

选取拥有此属性的元素

集合元素

$(“div[id]”)选取拥有属性id的元素

[attribute=value]

选取属性的值为value的元素

集合元素

$(“div[title=test]”)选取属性title”test”

元素

[attribute!=value]

选取属性的值不等于value的元素

集合元素

$(“div[title!=test]”)选取属性title不等于”test”

元素(注意:没有属性的title
元素也会被选取)

[attribute^=value]

选取属性的值以value开始的元素

集合元素

$(“div[title^=test]”)选取属性title”test”开始的

元素

[attribute$=value]

选取属性的值以value结束的元素

集合元素

$(“div[title$=test]”)选取属性title”test”结束的

元素

[attribute*=value]

选取属性值含有value的元素

集合元素

$(“div[title*=test]”)选取属性title含有”test”

元素

[selector1][selector2][selectorN]

用属性选择器合并成一个复合属性选择器,满足多个条件。每个选择一次,缩小一次范围

集合元素

$(“div[id][title$='tets']”)选取拥有属性id,并且属性title”test”结束的

元素

5-子元素过滤选择器

 

选择器

描述

返回

示例

:nth-child(index/even/odd/equation)

选取每个父元素下的第index个子元素或者奇偶元素(index1算起)

集合元素

:eq(index)只匹配一个元素,而:nth-child将为每一个父元素匹配子元素,并且:nth-child(index)index是从1开始的,而:eq(index)是从0算起

:first-child

选取每个父元素的第一个子元素

集合元素

:first只返回单个元素,而:first-child选择符将为每个父元素匹配第一个子元素

:last-child

选取每个父元素的最后一个子元素

集合元素

:last只返回单个元素,而:last-child选择符将为每个父元素匹配最后一个子元素

:only-child

如果某个元素是它父元素中唯一的子元素,那么将会被匹配。如果父元素中含有其他元素,则不会被匹配

集合元素

 

:nth-child()选择器详细功能

1:nth-child(even)能选取每个父元素下的索引值是偶数的元素。

2:nth-child(odd) 能选取每个父元素下的索引值是奇数的元素。

3:nth-child(2)能选取每个父元素下的索引值等于2的元素

4:nth-child(3n)能选取每个父元素下的索引值是3的倍数的元素,(n0开始)。

5:nth-child(3n+1)能选取每个父元素的索引值是(3n+1)的元素。(n0开始)

6-表单对象属性过滤选择器

 

选择器

描述

返回

示例

:enabled

选取所有可用元素

集合元素

$(“#form1:enabled”) ;选取id”form1”的表单内的所有可用元素

:disabled

选取所有不可用元素

集合元素

$(“#form1:disabled”) ;选取id”form1”的表单内的所有不可用元素

:checked

选取所有被选中的元素(单选框,复选框)

集合元素

$(“input:checked”);选取所有被选中的元素

:selected

选取所有被选中的选项元素(下拉列表)

集合元素

$(“select:selected”);选取所有被选中的选项元素

4. Form Selector

Table-Form object attribute filtering example

选择器

描述

返回

示例

:input

选取所有可用元素

集合元素

$(“:input”) 选取所有元素

:text

选取所有不可用元素

集合元素

$(“:text”) 选取所有的单行文本

:password

选取所有的密码框

集合元素

$(“: password”)选取所有的密码框

:radio

选取所有的单选框

集合元素

$(“:radio”)选取所有的单选框

:checkbox

选取所有的多选框

集合元素

$(“:checkbox”)选取所有的多选框

:submit

选取所有的提交按钮

集合元素

$(“:submit”)选取所有的提交按钮

:image

选取所有的图像按钮

集合元素

$(“:image”)选取所有的图像按钮

:reset

选取所有的重置按钮

集合元素

$(“:reset”)选取所有的重置按钮

:button

选取所有的按钮

集合元素

$(“:button”)选取所有按钮

:file

选取所有的上传域

集合元素

$(:file)选取所有的上传域

:hidden

选取所有不可见元素

集合元素

$(“:hidden”)选取所有不可见元素

Selector
Description Return Example

:input

Select all available elements

Collection elements

$(“:input”) Select all ,

:text

Select all unavailable elements

Collection elements

$(“:text”) Select all single lines of text

:password

Select all password boxes

Collection elements

$(“: password”)Select all password boxes

:radio

Select all radio boxes

Collection elements

$(“:radio”)Select all radio boxes

:checkbox

Select all checkboxes

Collection elements

$(“:checkbox”)Select all checkboxes

:submit

Select all submit buttons

Collection elements

$(“:submit”)Select all submit buttons

:image

Select all images button

Collection elements

$(“:image”)Select all image buttons

:reset

Select all reset buttons

Collection elements

$(“:reset”)Select all reset buttons

:button

Select all buttons

Collection elements

$(“:button”)Select all buttons

:file

Select all upload domains

Collection elements

$(:file)Select all upload domains

:hidden

Select all invisible elements

Collection elements

$(“:hidden”)Select all invisible elements

Zi Moyan: My life has a limit, but I know it has no limit...
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
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

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.

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

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),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools