Overview of jQuery's element filtering
1, eq() Filter out the element with the specified index number
2, first() Filter out the first matching element
3, last() Filter out the last matching element
4, hasClass() Check whether the matching element Contains the specified class
5, filter() Filter out the set of elements that match the specified expression
6, is() Check whether the element can match the parameter
7, map()
8, has() Filter out the set containing Specify the elements of the child element
9, not() Exclude elements that can be matched in the parameters
10, slice() Starting from the specified index, intercept the specified number of elements
11, children() Filter to obtain the resources of the specified element
12 , closest() Starting from the current element, return the first matching parent element that meets the conditions
13. find() Find the child element from the specified element
14. next() Get the next sibling element of the specified element
15. nextAll() Get all subsequent sibling elements
16, nextUntil() Get the subsequent elements until the parameters can match, excluding the end condition
17, offsetPosition() Return the first ancestor used for positioning Element, that is, find the element whose position is relative or absolute among the ancestor elements.
18. parent() Get the direct parent element of the specified element
19. parents() Get all the ancestor elements of the specified element, all the way to
20. parentsUntil() Get the ancestor elements of the specified element , until the parameters can be matched
21. prev() Get the previous sibling element of the specified element
22. prevAll() Get all the sibling elements before the specified element
23. prevUntil() Get all the brothers before the specified element elements until the conditions in the parameters can be matched. Note that the parameter condition itself will not be matched
24. siblings() Get the sibling elements of the specified element, regardless of before or after
25. add() Add the selected element to the jQuery object collection
26. andSelf() Add itself to In the selected jQuery collection, to facilitate one-time operations
27. end() Will roll back the operation that changes the selection of the current selector to the previous state.
28, contents Not understood
****************************** Filter****************** **********************
1. eq() Filter the elements with the specified index number
Syntax: eq(index|-index) The index number starts from 0, If it is a negative value, count down from the last one, and the last one starts from -1
$("p").eq(1); //如果选择器改为 $("p").eq(-1),则我是第四个P会被选中 <div> <p>我是第一个P</p> <p>我是第二个P</p> //会被选中,索引值为1 <p>我是第三个P</p> <p>我是第四个P</p> </div>
2. first() Filter out the first matching element
Syntax: first() This method No parameters
$("p").first(); <div> <p>我是第一个P</p> //我的索引值是0,我是第一个,我会被选中 <p>我是第二个P</p> <p>我是第三个P</p> <p>我是第四个P</p> </div>
3. last() Filter out the last matching element
Syntax: last() This method has no parameters
$("p").last(); <div> <p>我是第一个P</p> <p>我是第二个P</p> <p>我是第三个P</p> <p>我是第四个P</p> //我是最后一个,我会被选中 </div>
4. hasClass() Check whether the matching element contains Specified class
Syntax: hasClass(class) Class is the category name //returns a Boolean value
if($("p").hasClass("p2")) { alert("我里面含有class=p2的元素"); //会弹出,p里的确存在class="p2"的元素 } <div> <p>我是第一个P</p> <p class="p2">我是第二个P</p> <p>我是第三个P</p> <p>我是第四个P</p> </div>
5. filter() Filter out the set of elements that match the specified expression
Syntax: filter (expr|obj|ele|fn) expr: matching expression | obj: jQuery object, used to match existing elements | DOM: DOM element used for matching | function return value as matching condition
$("p").filter(".p2"); <div> <p>我是第一个P</p> <p class="p2">我是第二个P</p> //我会被选中,我的class="p2" <p>我是第三个P</p> <p>我是第四个P</p> </div>
6. is() Check whether the element can be matched in the parameters
Syntax: is(expr|obj|ele|fn) expr: matching expression | obj: jQuery object, used to match existing elements | DOM: DOM element used for matching | function return value as matching condition
$($("p").first().is(".p2")) { alert("不会弹出,因为第一个P的class不等于p2"); } <div> <p>我是第一个P</p> <p class="p2">我是第二个P</p> //我会被选中,我的class="p2" <p>我是第三个P</p> <p>我是第四个P</p> </div>
7. map()
8. has() Filter out the elements containing the specified sub-element
Syntax: has(expr|ele) expr: selection expression | DOM element selection
$("div").has("p"); <div> //本div会被选中,因为该div含有p子元素 <p>我是第一个P</p> <p class="p2">我是第二个P</p> <p>我是第三个P</p> <p>我是第四个P</p> </div> <div> <span>我是一个span</spam> </div>
9. not() Exclude elements that can be matched in the parameters
Syntax: not(expr|ele|fn) expr: selection expression | DOM element selection | The function of fn is not clear yet
$("p").not(".p2"); <div> <p>我是第一个P</p> //会被选中,没有class=p2 <p class="p2">我是第二个P</p> //不会被选中,因为有class=p2被not(".p2")排除了 <p>我是第三个P</p> //会被选中,没有class=p2 <p>我是第四个P</p> //会被选中,没有class=p2 </div>
10. slice() Starting from the specified index, intercept the specified number of elements
Syntax: slice(start, [end]) Start position, end can Select the end position, excluding the end position. If not specified, the last one is matched.
$("p").slice(1,3) <div> <p>我是第一个P</p> //不会被选中,我索引为0 <p class="p2">我是第二个P</p> //会被选中,我索引为1 <p>我是第三个P</p> //会被选中,我索引为2 <p>我是第四个P</p> //不会被选中,虽然我的索引为3,但是不包括我 </div>
************************ Filter************************ *************
11. children() Filter to obtain the resources of the specified element
Syntax: children(expr); Get the resources of the specified element, expr is the filtering condition for the child element
$("div").children(".p2"); <div> <p>我是第一个P</p> //不会被选中,虽然我是div的子元素,但是我没class=p2 <p class="p2">我是第二个P</p> //会被选中,我既是p的子元素,又有class=p2 <p>我是第三个P</p> //不会被选中,虽然我是div的子元素,但是我没class=p2 <p>我是第四个P</p> //不会被选中,虽然我是div的子元素,但是我没class=p2 </div>
12. closest() Starting from the current element, return the first matching parent element that meets the conditions
$("span").closest("p","div"); <div> //不会被选中,被P抢了先机 <p>我是第一个P //P会被选中,因为P符合条件,而且是最先匹配到的,虽然div也符合条件了,但是div不是最先匹配到的。因此div不会被选中。 <span>我是P里的span</span> </p> </div>
13. find() Find child elements from the specified element
Syntax: find(expr|obj|ele) expr: matching expression | obj jQuery object used for matching | DOM element
$("div").find(".p2"); <div> <p>我是第一个P</p> //不会被选中,虽然我是div的子元素,但是我没class=p2 <p class="p2">我是第二个P</p> //会被选中,我既是p的子元素,又有class=p2 <p>我是第三个P</p> //不会被选中,虽然我是div的子元素,但是我没class=p2 <p>我是第四个P</p> //不会被选中,虽然我是div的子元素,但是我没class=p2 </div>
十四、next() 获取指定元素的下一个兄弟元素
语法:next(expr) expr:可选,筛选条件,如果下一个兄弟元素不符合改条件,则返回空。
$(".p2").next(); //如果筛选改为$(".p2").next(".p4")那选中的是哪个呢?答案是:没选中任何元素,因为虽然有个class=p4的P,但它不是.p2的下一个。 <div> <p>我是第一个P</p> <p class="p2">我是第二个P</p> <p>我是第三个P</p> //我是.p2的next <p class="p4">我是第四个P</p> </div>
十五、nextAll() 获取其后的所有兄弟元素
语法:nextAll(expr) expr:可选,筛选条件,获取其后符合expr条件的所有兄弟元素
$(".p2").nextAll(); //如果筛选条件改为 $(".p2").nextAll(".p4"); 则只有我是第四个P会被选中 <div> <p>我是第一个P</p> <p class="p2">我是第二个P</p> <p>我是第三个P</p> //会被选中,是.p2后面的兄弟元素 <p class="p4">我是第四个P</p> //会被选中,是.p2后面的兄弟元素 </div>
十六、nextUntil() 获取其后的元素,直到参数能匹配上的为止,不包括结束条件那个
语法:nextUntil([expr|ele][,fil]) expr筛选表达式 | DOM元素筛选,注意不包括参数里的那一个
$(".p2").nextUntil(".p4"); //注意此方法并不会包括.p4 <div> <p>我是第一个P</p> <p class="p2">我是第二个P</p> <p>我是第三个P</p> //会被选中,是.p2后面的兄弟元素 <p class="p4">我是第四个P</p> //不会被选中,我作为结束条件,但不包括我 </div>
十七、offsetPosition() 返回第一个用于定位的祖先元素,即查找祖先元素中position为relative或absolute的元素。
语法:offsetPosition() 此方法没有参数 由于CSS的绝对定位的定位基准是相对最近的一个已定位元素,因此此方法的作用不言而喻。
$("span").offsetParent(); <div style="position:relative"> //选中的是div,因此div是已定位元素。 <p> <span>我是一个span</span> </p> </div>
十八、parent() 获取指定元素的直接父元素
语法:parent(expr) expr为筛选条件,如果直接父元素不符合条件,则不返回任何元素(无论它的祖先是否具有能与expr匹配的)
$("span").parent(); <div style="position:relative"> <p> //我是span的直接父元素,我会被匹配到 <span>我是一个span</span> </p> </div>
十九、parents() 获取指定元素的所有祖先元素,一直到
语法:parents(expr) expr为筛选条件,如果某个祖先元素不符合expr则排除
$("span").parents(); <div style="position:relative"> //我是span的祖先元素,我也会被匹配到.另外<body></body>也会被匹配到 <p> //我是span的直接父元素,我会被匹配到 <span>我是一个span</span> </p> </div>
二十、parentsUntil() 获取指定元素的祖先元素,知道参数里能匹配到的为止
语法:parentsUntil(expr) expr为停止参数,一直匹配到expr为止,同时参数的条件是不会被匹配中的。
$("span").parentsUntil("div"); <div style="position:relative"> //我是span的祖先元素,但是我作为停止条件,我也不会被选中 <p> //我是span的直接父元素,我会被选中 <span>我是一个span</span> </p> </div>
二十一、prev() 获取指定元素的前一个兄弟元素
语法:prev(expr) expr:可选。当上一个兄弟元素不符合参数中的条件时,不返回任何元素。
$(".p2").prev(); <div> <p>我是第一个P</p> //我会被选中,我是.p2的前一个元素。 <p class="p2">我是第二个P</p> <p>我是第三个P</p> <p class="p4">我是第四个P</p> </div>
二十二、prevAll() 获取指定元素前面的所有兄弟元素
语法:prevAll(expr) expr:可选,排除所有不能够被expr匹配上的元素
$(".p4").prevAll(".p2"); <div> <p>我是第一个P</p> //不会被选中,虽然我是.p4前面的兄弟元素,但是我没有class=p2 <p class="p2">我是第二个P</p> //会被选中,我既是.p4前面的兄弟元素,而且我有class=p2 <p>我是第三个P</p> //不会被选中,虽然我是.p4前面的兄弟元素,但是我没有class=p2 <p class="p4">我是第四个P</p> </div>
二十三、prevUntil() 获取指定元素前面的所有兄弟元素,直到参数里的条件能够匹配到的。 注意参数条件本身不会被匹配
语法:prevUntil([expr|ele][,fil]) expr匹配表达式 | DOM元素匹配
$(".p4").prevUntil(".p2"); <div> <p>我是第一个P</p> //不会被选中,到p2就停止了 <p class="p2">我是第二个P</p> //不会被选中,我是停止条件,不包括我 <p>我是第三个P</p> //会被选中,我在.p2前,递归到我在到.p2 <p class="p4">我是第四个P</p> //不会被选中,我自己怎么可能是我自己前面的呢? </div>
/******************** 串联 *******************************/
二十四、siblings() 获取指定元素的兄弟元素,不分前后
语法:siblings(expr); expr为筛选条件,不符合条件的不会选中
$(".p2").siblings(); <div> <p>我是第一个P</p> //会被选中,我是.p2的兄弟元素 <p class="p2">我是第二个P</p> //不会被选中,我是自己 <p>我是第三个P</p> //会被选中,我是.p2的兄弟元素 <p class="p4">我是第四个P</p> //会被选中,我是.p2的兄弟元素 </div>
二十五、add() 将选中的元素添加到jQuery对象集合中
add(expr|elements|html|jQueryObject) expr:选择器表达式 | DOM表达式 | Html片段 | jQuery对象,将jQuery对象集合一起方便操作;
$(".p2").add("span").css("background-color","red"); <div> <p>我是第一个P</p> <p class="p2">我是第二个P</p> //会变红 <p>我是第三个P</p> <p class="p4">我是第四个P</p> </div> <span>我是一个span</span> //会变红
二十六、andSelf() 将自身加到选中的jQuery集合中,以方便一次性操作
andSelf() 此方法无参数
$(".p2").nextAll().andSelf().css("background-color","red"); <div> <p>我是第一个P</p> <p class="p2">我是第二个P</p> //会变红,这就是andSelf()的效果 <p>我是第三个P</p> //会变红 <p class="p4">我是第四个P</p> //会变红 </div>
二十七、end() 将改变当前选择器选中的操作回退为上一个状态。
end() 此方法没有参数
$(".p2").next().end().css("background-color","red"); <div> <p>我是第一个P</p> <p class="p2">我是第二个P</p> //会变红,这就end()的效果 <p>我是第三个P</p> //不会变红,尽管next()方法之后选中的是这一个,但是由于被end()方法回退了因此是上一个。 <p class="p4">我是第四个P</p> </div>

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:

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
