Detailed explanation of jQuery selector_jquery
Selectors are the most basic thing in jQuery. The selectors listed in this article basically include all jQuery selectors. Maybe you can deepen your understanding of jQuery selectors through this article. Their usage is very simple. What I hope more is that it can improve the efficiency of writing jQuery code for individuals. This article introduces all jQuery selectors with screenshots, code and a simple summary, and also lists some areas that need attention and distinction.
1. Basic selector
1. id selector (specify id element)
Set the background color of the element with id="one" to black. (id selector returns a single element)
1 |
|
2. class selector (traverse css class elements)
Set the background color of the element with class="cube" to black
1 |
|
3. element selector (traverse html elements)
Set the text size of the p element to 12px
1 |
|
4. * Selector (traverse all elements)
1 |
|
5. Column Selector
1 2 3 4 |
|
2. Level Selector
1. parent > child (direct child element)
1 2 3 4 |
|
In the following code, only the first span will change color. The second span does not belong to the first generation of child elements of the div, and the color remains unchanged.
1 2 3 4 5 6 |
|
2. prev next (next sibling element, equivalent to next() method)
1 2 3 4 5 |
|
In the code below, only 123 and 789 will change color
1 |
|
3. prev ~ siblings (all sibling elements of the prev element, equivalent to the nextAll() method)
1 |
|
With the following code, G2 and G4 will change color
1 |
|
3. Filter selector
1. Basic filter selector
——1.1 :first and :last (take the first element or the last element)
1 |
|
With the following code, G1 (first element) and G3 (last element) will change color
1 |
|
——1.2:not (take non-element)
1 |
|
The following code, G1 will change color
1 |
|
However, please note the following code:
1 2 3 4 |
|
When the div where G1 is located and the div where G2 is located have a parent-child relationship, both G1 and G2 will change color.
——1.3 :even and :odd (take even index or odd index element, index starts from 0, even means even number, odd means odd number)
1 |
|
The color of rows A and C is #EEE (the index of the first row is 0), the color of rows B and D is #DADADA
1 |
|
A |
B |
C |
D |
——1.4 :eq(x) (take the element at the specified index)
1 |
|
Change the background color of the third line. In the above code, the background of C will change color.
——1.5 :gt(x) and :lt(x) (take elements greater than x index or less than x index)
1 |
|
L4 and L5 will be red, L1 and L2 will be blue, L3 is the default color
1 |
|
- L1
- L2
- L3
- L4
- L5
——1.6:header (take H1~H6 title elements)
1 |
|
With the following code, the background color of H1~H6 will change
1 |
|
2. Content filter selector
——2.1:contains(text) (get the element containing text)
1 2 3 4 |
|
In the following code, the first dd will change color
1 |
|
- 技术
- jQuery, .NET, CLR
- SEO
- 关键字排名
- 其他
——2.2 :empty (take elements that do not contain child elements or have empty text)
1 |
|
The third dd above will display the text "No content"
——2.3:has(selector) (take the element matched by the selector)
1 |
|
Even if span is not a direct child element of div, it will take effect
1 2 3 4 5 |
|
——2.4 :parent(取包含子元素或文本的元素)
1 |
|
下面的代码,A和D所在的li会有边框
1 2 3 4 5 6 7 |
|
3. 可见性过滤选择器
——3.1 :hidden(取不可见的元素)
jQuery至1.3.2之后的:hidden选择器仅匹配display:none或的元素,而不匹配visibility: hidden或opacity:0的元素。这也意味着hidden只匹配那些“隐藏的”并且不占空间的元素,像visibility:hidden或 opactity:0的元素占据了空间,会被排除在外。
参照:http://www.jquerysdk.com/api/hidden-selector
下面的代码,先弹出"hello"对话框,然后hid-1会显示,hid-2仍然是不可见的。
1 |
|
——3.2 :visible(取可见的元素)
下面的代码,最后一个div会有背景色
1 2 |
|
4. 属性过滤选择器
——4.1 [attribute](取拥有attribute属性的元素)
下面的代码,最后一个a标签没有title属性,所以它仍然会带下划线
1 |
|
——4.2 [attribute = value]和[attribute != value](取attribute属性值等于value或不等于value的元素)
分别为class="item"和class!=item的a标签指定文字颜色
1 |
|
——4.3 [attribute ^= value], [attribute $= value]和[attribute *= value](attribute属性值以value开始,以value结束,或包含value值)
在属性选择器中,^$符号和正则表达式的开始结束符号表示的含义是一致的,*模糊匹配,类似于sql中的like '%str%'。
1 |
|
——4.4 [selector1][selector2](复合型属性过滤器,同时满足多个条件)
将title以"jQuery"开始,并且class="item"的a标签隐藏,那么jQuery事件大全会被隐藏
1 |
|
5. 子元素过滤选择器
——5.1 :first-child和:last-child
:first-child表示第一个子元素,:last-child表示最后一个子元素。
需要大家注意的是,:fisrst和:last返回的都是单个元素,而:first-child和:last-child返回的都是集合元素。举个 例子:div:first返回的是整个DOM文档中第一个div元素,而div:first-child是返回所有div元素下的第一个元素合并后的集 合。
这里有个问题:如果一个元素没有子元素,:first-child和:last-child会返回null吗?请看下面的代码:
1 2 3 4 5 6 |
|
也许你觉得这个答案,是不是太简单了?len1 = 2, len2 = 2。但实际确并不是,它们俩都等于3。
把上面的代码稍微修改一下:
1 2 3 4 |
|
结果却是弹出三个alert,只不过最后一个alert里面是空白的。
——5.2 :only-child
当某个元素有且仅有一个子元素时,:only-child才会生效。
1 2 3 4 |
|
这里:only-child也是三个元素,从最后一个很粗的红色边框(实际是两个元素的边框重叠了)也可以看出来。
——5.3 :nth-child
看到这个就想起英文单词里的,fourth, fifth, sixth……,nth表示第n个,:nth-child就表示第n个child元素。要注意的是,这儿的n不像eq(x)、gt(x)或lt(x)是从 0开始的,它是从1开始的,英文里好像也没有zeroth这样的序号词吧。
:nth-child有三种用法:
1) :nth-child(x),获取第x个子元素
2) :nth-child(even)和:nth-child(odd),从1开始,获取第偶数个元素或第奇数个元素
3) :nth-child(xn+y),x>=0,y>=0。例如x = 3, y = 0时就是3n,表示取第3n个元素(n>=0)。实际上xn+y是上面两种的通项式。(当x=0,y>=0时,等同于:hth- child(x);当x=2,y=0时,等同于nth-child(even);当x=2,y=1时,等同于:nth-child(odd))
下面的两个例子是针对2)和3)的,1)的例子我就不列举了。
例2:
1 |
|
1. NBA 2012季后赛 |
2. NBA 2011季后赛 |
3. NBA 2010季后赛 |
4. NBA 2009季后赛 |
5. NBA 2008季后赛 |
6. NBA 2007季后赛 |
例3(html代码和例2是一样的):
1 |
|
6. 表单对象属性过滤选择器
——6.1 :enabled和:disabled(取可用或不可用元素)
:enabled和:diabled的匹配范围包括input, select, textarea。
1 2 3 4 5 6 7 |
|
——6.2 :checked(取选中的单选框或复选框元素)
下面的代码,更改边框或背景色仅在IE下有效果,chrome和firefox不会改变,但是alert都会弹出来。
1 2 |
|
——6.3 :selected(取下拉列表被选中的元素)
1 2 3 4 |
|
四、表单选择器
1. :input(取input,textarea,select,button元素)
:input元素这里就不再多说了,前面的一些例子中也已经囊括了。
2. :text(取单行文本框元素)和:password(取密码框元素)
这两个选择器分别和属性选择器$('input[type=text]')、$('input[type=password]')等同。
1 |
|
1 |
|
3. :radio(取单选框元素)
:radio选择器和属性选择器$('input[type=radio]')等同
1 |
|
4. :checkbox(取复选框元素)
:checkbox选择器和属性选择器$('input[type=checkbox]')等同
1 |
|
上面的代码,会将所有额checkbox的value输出出来。若你想选择选中项,有三种写法:
1 |
|
5. :submit(取提交按钮元素)
:submit选择器和属性选择器$('input[type=submit]')等同
6. :reset(取重置按钮元素)
:reset选择器和属性选择器$('input[type=reset]')等同
7. :button(取按钮元素)
:button选择器和属性选择器$('input[type=button]')等同
8. :file(取上传域元素)
:file选择器和属性选择器$('input[type=file]')等同
9. :hidden (retrieve invisible elements)
: The hidden selector is equivalent to the attribute selector $('input[type=hidden]')
The above is the entire content of jQuery selector, is it very comprehensive? If there is anything missing, please let me know and this article will continue to be updated.

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

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

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

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:

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

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

目录1:basename()2:copy()3:dirname()4:disk_free_space()5:disk_total_space()6:file_exists()7:file_get_contents()8:file_put_contents()9:filesize()10:filetype()11:glob()12:is_dir()13:is_writable()14:mkdir()15:move_uploaded_file()16:parse_ini_file()17:
