Learning the selector of jQuery source code
这篇文章主要介绍了关于jQuery源码之选择器的学习 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
选择器
一、选择器分类
jQuery的选择器和CSS的选择器非常相似
大致可以分为几类: 基本筛选器: eq get first lang It not odd root... 内容筛选器: contains empty has parent... 可见筛选器: hidden visible 子元素筛选器: first-child nth-child only-child... 表单: bottom checkbox foucs input text...
二、选择器API
id
$('#app') /* 如果含有特殊字符 */ $('#app\\:ip') ==> id="app:ip"
class
$('.class')
element
$('p')
*
$('*') /*匹配全部 */
selector1,selector2,selector3
$('p,#p,.class,span.love')
parent selector (祖先和后代的关系)
/* 指定的祖先元素下的所有的后代元素 */ <form> <label>Name:</label> <input name="name" /> <fieldset> <label>Newsletter:</label> <input name="newsletter" /> </fieldset> </form> <input name="none" /> $('form input') /* result */ /* [ <input name="name" />, <input name="newsletter" /> ] */
parent > child (父亲和儿子的关系)
/* 匹配父元素下的所有的子元素 */ <form> <label>Name:</label> <input name="name" /> <fieldset> <label>Newsletter:</label> <input name="newsletter" /> </fieldset> </form> <input name="none" /> $('form > input') /* result */ /* [ <input name="name" /> ] */
prev + next (下一个兄弟关系)
/* 匹配所有跟在prev后面的下一个元素 */ <form> <label>Name:</label> <input name="name" /> <fieldset> <label>Newsletter:</label> <input name="newsletter" /> </fieldset> </form> <input name="none" /> $('label + input') /* result */ /* [ <input name="name" />, <input name="newsletter" /> ] */
prev ~ siblings (下面的所有兄弟元素)
/* 匹配prev后面的所有的兄弟元素 */ <form> <label id="name">Name:</label> <input name="name" /> <fieldset> <label>Newsletter:</label> <input name="newsletter" /> </fieldset> <input name="none" /> <input name="sex" /> </form> $('#name ~ input') /* result */ /* <input name="name" /> <input name="none" /> <input name="sex" /> */
:first
获取匹配的第一个元素
$('input:first') $('ul li:first'); // 捕获到同类型元素后,在取其第一个
:not(selector)
去除选定的selector那部分
// 去除已选择的元素中的部分 $('input:not(:checked)') <input name="apple" /> <input name="flower" checked="checked" /> // result // [ <input name="apple" /> ]
:even(index)
// 匹配索引为偶数的,从 0 开始计数(将0包含进even) // 第 1,3,5,7 行 // $('tr:even')
:odd(index)
// 匹配索引为奇数的 // 第2,4,6,8 行
:eq(index)
// 匹配给定一个索引 $('tr:eq(1)')
:gt(index)
// 匹配大于索引值的项 $('tr:gt(1)')
:lang(language) 1.9+
// 匹配指定语言的元素 $('p:lang(en)') // 选择器$("p:lang(en)")将匹配<p lang="en"> and <p lang="en-us">(和他们的后代<p>),但不包括<p lang="fr">
:last
// 获取匹配的最后个元素 $('p:last')
:lt(index)
// 匹配索引小于指定值 // $('p:lt(4)')
:header
// 匹配所有的标题元素 // h1 h2 h3 h4 h5 h6 $(":header").css("background", "#EEE");
:animated
// 匹配所有正在执行动画的元素 <button id="run">Run</button><p></p> $('p:not(:animated)').animate({ left: '+=20px' },1000);
:focus 1.6+
// 匹配当前获取焦点的元素。 $('input:focus').css("background":"#ccc");
:root 1.9+
// 匹配页面的根元素 $(':root').css("background":"yellow"); // 设置<html>背景颜色为黄色
:target 1.9+
// 如果url中包含有http://example.com/#foo $('a:target') // 就是选中了 <a id="foo"></a>
:contains(text)
// 匹配包含给定文本的 $('p:contains('join')');
:empty()
$('p:empty') // 匹配不包含子元素或文本内容 <table> <tr><td>Value 1</td><td></td></tr> <tr><td>Value 2</td><td></td></tr> </table> $('td:empty') // [ <td></td>, <td></td> ]
:has()
// 匹配含有has内部选择器选中的元素的元素 $('p:has('p')')
:parent 与empty相反
// 匹配含有子元素或者文本内容的 $('td:parent') <table> <tr><td>Value 1</td><td></td></tr> <tr><td>Value 2</td><td></td></tr> </table> // <td>Value 1</td><td>Value 2</td>
:hidden
// 匹配不可见的元素 // $('input:hidden')
:visable
// 匹配可见的元素 <table> <tr style="display:none"><td>Value 1</td></tr> <tr><td>Value 2</td></tr> </table> $('tr:visable') // <tr><td>Value 2</td></tr> ]
[attribute]
// 匹配包含给定属性的元素 $('input[name]')
[attribute=value]
// 匹配给定的属性是某个特定值的元素 $('input[name="sex"]')
[attribute!=value]
[attribute^=vlaue]
// 匹配属性以value开头
[attribute$=value]
// 匹配属性以value结尾
[attribute*=value]
// 匹配属性包含某些值的元素
selector1[selector3]
// 匹配同时满足多个属性选择器的元素 $("input[id][name$='man']")
:first-child
// 匹配所给选择器( :之前的选择器)的第一个子元素,最终的结果可能是多个,不同于:first 之处是,:first是指匹配到的元素(:之前的元素)的第一个。 <ul> <li>John</li> <li>Karl</li> <li>Brandon</li> </ul> <ul> <li>Glen</li> <li>Tane</li> <li>Ralph</li> </ul> $('ul li:first-child'); // [ <li>John</li>, <li>Glen</li> ]
:first-of-type
// [1] $('span:first-of-type') // 匹配到span元素,而且这个span元素是其父级的第一个span <p id="n1"> <p id="n2" class="abc"> <label id="n3">label1</label> <span id="n4">span1</span> <span id="n5" class="abc">span2</span> <span id="n6">span3</span> </p> <p id="n7"> <span id="n8" class="abc">span1</span> <span id="n9">span2</span> </p> </p> // <span id="n4">span1</span> <span id="n8" class="abc">span1</span> // 【2】 $('.abc:first-of-type') <p id="n1"> <p id="n2" class="abc"> <label id="n3">label1</label> <span id="n4">span1</span> <span id="n5" class="abc">span2</span> <span id="n6">span3</span> </p> <p id="n7"> <span id="n8" class="abc">span1</span> <span id="n9">span2</span> </p> </p> // <p id="n2" class="abc"></p> <span id="n8" class="abc">span1</span>
:last-child
同理:first-child
:last-of-type
:nth-child(n)
n可以是:
序号、even、odd、(3n+2)
(3n+2)表示从第二个开始,匹配3的倍数的元素
// 这里的n是从1 开始的,跟:first-child类似 <ul> <li>John</li> <li>Karl</li> <li>Brandon</li> </ul> <ul> <li>Glen</li> <li>Tane</li> <li>Ralph</li> </ul> $('ul li:nth-child(2)') // <li>Karl</li><li>Tane</li>
:nth-last-child(n)
n可以是:
序号、even、odd、(3n+2)
(3n+2)表示从第二个开始,匹配3的倍数的元素
跟:nth-child(n) 类似,只是它是从后往前算的
only-child
// 如果某个元素是父元素中唯一的子元素,那将会被匹配 <ul> <li>John</li> <li>Karl</li> <li>Brandon</li> </ul> <ul> <li>Glen</li> </ul> $('ul li:only-child') // <li>Glen</li>
:input
匹配所有 input, textarea, select 和 button 元素
<form> <input type="button" value="Input Button"/> <input type="checkbox" /> <input type="file" /> <input type="hidden" /> <input type="image" /> <input type="password" /> <input type="radio" /> <input type="reset" /> <input type="submit" /> <input type="text" /> <select><option>Option</option></select> <textarea></textarea> <button>Button</button> </form> // 全选$(':input')
:text
<form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input type="submit" /> <input type="reset" /> <input type="password" /> <input type="button" /> <select><option/></select> <textarea></textarea> <button></button> </form> $(':text') // <input type="text" />
:password
<form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input type="submit" /> <input type="reset" /> <input type="password" /> <input type="button" /> <select><option/></select> <textarea></textarea> <button></button> </form> $(':password') // <input type="password" />
:radio
<form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input type="submit" /> <input type="reset" /> <input type="password" /> <input type="button" /> <select><option/></select> <textarea></textarea> <button></button> </form> $(':radio') // <input type="radio" />
:submit
<form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input type="submit" /> <input type="reset" /> <input type="password" /> <input type="button" /> <select><option/></select> <textarea></textarea> <button></button> </form> $(':submit') // <input type="submit" />
:image
<form> <input type="text" /> <input type="checkbox" /> <input type="radio" /> <input type="image" /> <input type="file" /> <input type="submit" /> <input type="reset" /> <input type="password" /> <input type="button" /> <select><option/></select> <textarea></textarea> <button></button> </form> $(':image') // <input type="image" />
:reset:button
:file
:enabled
选择可用的元素
<form> <input name="email" disabled="disabled" /> <input name="id" /> </form> $("input:enabled") // <input name="id" />
:disabled
选择不能使用的
:checked
$("input:checked")
:selected
$('option:selected')
三、css解析原理
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
The above is the detailed content of Learning the selector of jQuery source code. For more information, please follow other related articles on the PHP Chinese website!

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

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute
