Home > Article > Web Front-end > How to filter exclude elements using jQuery to modify properties of specified tags
Simple case:
$(function(){ $("td[id][id!='']").click(function(){ //你的逻辑 }); });
In the above code, any td that has an id and the id is not empty will execute "your logic".
========================Reprint====================== ===
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 Element
4, hasClass() Check whether the matched 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 be specified in the parameter Matching
7, map()
8, has() Filter out elements containing specified subelements
9, not() Exclude elements that can be matched in 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 child elements 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 match, excluding the end condition
17, offsetPosition() Return the first ancestor element used for positioning, that is, find the ancestor element whose position is relative or absolute element.
18. parent() Get the direct parent element of the specified element
19. parents() Get all the ancestor elements of the specified element until 6c04bd5ca3fcae76e30b72ad730ca86d36cc49f0c466276486e50c850b7e4956
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() Gets all sibling elements in front of the specified element 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 the selected jQuery collection to facilitate one-time operations
27, end() Return the operation that changes the selection of the current selector to the previous state.
28、contents Not understood
****************************** Filter************ ****************************
1. eq() Filter elements with specified index number
Syntax :eq(index|-index) The index number starts from 0. If it is a negative value, it counts down from the last one, and the last one starts from -1
$("p").eq(1); //如果选择器改为 $("p").eq(-1),则我是第四个P会被选中 e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第二个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,索引值为1 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第四个P94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3
Syntax: first() This method has no parameters
$("p").first(); e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 //我的索引值是0,我是第一个,我会被选中 e388a4556c0f65e1904146cc1a846bee我是第二个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第四个P94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3
Syntax: last() This method has no parameters
$("p").last(); e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第二个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第四个P94b3e26ee717c64999d7867364b1b4a3 //我是最后一个,我会被选中 94b3e26ee717c64999d7867364b1b4a3
Syntax: hasClass(class) Class is the category name //returns a Boolean value
if($("p").hasClass("p2")) { alert("我里面含有class=p2的元素"); //会弹出,p里的确存在class="p2"的元素 } e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第四个P94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3
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"); e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 //我会被选中,我的class="p2" e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第四个P94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3
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"); } e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 //我会被选中,我的class="p2" e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第四个P94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3
7. map ()
8. has() Filter out elements containing specified sub-elements
Syntax: has(expr|ele) expr: selection expression | DOM element selection
$("p").has("p"); e388a4556c0f65e1904146cc1a846bee //本p会被选中,因为该p含有p子元素 e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第四个P94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee 45a2772a6b6107b401db3c9b82c049c2我是一个span5d8d27040b30e9f46281915572ea3452 94b3e26ee717c64999d7867364b1b4a3
9. not() Exclude elements that can be matched in parameters
Syntax: not(expr|ele|fn) expr: selection expression|DOM element selection|The role of fn is not clear
$("p").not(".p2"); e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,没有class=p2 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,因为有class=p2被not(".p2")排除了 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,没有class=p2 e388a4556c0f65e1904146cc1a846bee我是第四个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,没有class=p2 94b3e26ee717c64999d7867364b1b4a3
10. slice() Starting from the specified index, intercept the specified number of elements
Syntax: slice(start, [end]) Start position, end is optional, end position, excluding the end position . If not specified, the last one is matched.
$("p").slice(1,3) e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,我索引为0 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,我索引为1 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,我索引为2 e388a4556c0f65e1904146cc1a846bee我是第四个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,虽然我的索引为3,但是不包括我 94b3e26ee717c64999d7867364b1b4a3
************************ 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 child elements
$("p").children(".p2"); e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,虽然我是p的子元素,但是我没class=p2 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,我既是p的子元素,又有class=p2 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,虽然我是p的子元素,但是我没class=p2 e388a4556c0f65e1904146cc1a846bee我是第四个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,虽然我是p的子元素,但是我没class=p2 94b3e26ee717c64999d7867364b1b4a3
12. closest() Starting from the current element, return the first matching parent element that meets the conditions
$("span").closest("p","p"); e388a4556c0f65e1904146cc1a846bee //不会被选中,被P抢了先机 e388a4556c0f65e1904146cc1a846bee我是第一个P //P会被选中,因为P符合条件,而且是最先匹配到的,虽然p也符合条件了,但是p不是最先匹配到的。因此p不会被选中。 45a2772a6b6107b401db3c9b82c049c2我是P里的span54bdf357c58b8a65c66d7c19c8e4d114 94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3
13. find() Starting from the specified Find sub-elements in elements
Syntax: find(expr|obj|ele) expr: matching expression | obj jQuery object used for matching | DOM element
$("p").find(".p2"); e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,虽然我是p的子元素,但是我没class=p2 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,我既是p的子元素,又有class=p2 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,虽然我是p的子元素,但是我没class=p2 e388a4556c0f65e1904146cc1a846bee我是第四个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,虽然我是p的子元素,但是我没class=p2 94b3e26ee717c64999d7867364b1b4a3
14. next() Get the next sibling element of the specified element
Syntax: next(expr) expr: Optional, filtering conditions, if the next sibling element does not meet the conditions, return empty.
$(".p2").next(); //如果筛选改为$(".p2").next(".p4")那选中的是哪个呢?答案是:没选中任何元素,因为虽然有个class=p4的P,但它不是.p2的下一个。 e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 //我是.p2的next d42a450727133d488599a1f95fad5570我是第四个P94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3
15. nextAll() Get all subsequent sibling elements
Syntax: nextAll(expr) expr: Optional, filter conditions, get all subsequent sibling elements that meet expr conditions
$(".p2").nextAll(); //如果筛选条件改为 $(".p2").nextAll(".p4"); 则只有我是第四个P会被选中 e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,是.p2后面的兄弟元素 d42a450727133d488599a1f95fad5570我是第四个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,是.p2后面的兄弟元素 94b3e26ee717c64999d7867364b1b4a3
16. nextUntil() Get the subsequent elements until the parameters match, excluding the end condition
语法:nextUntil([expr|ele][,fil]) expr筛选表达式 | DOM元素筛选,注意不包括参数里的那一个
$(".p2").nextUntil(".p4"); //注意此方法并不会包括.p4 e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,是.p2后面的兄弟元素 d42a450727133d488599a1f95fad5570我是第四个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,我作为结束条件,但不包括我 94b3e26ee717c64999d7867364b1b4a3
十七、offsetPosition() 返回第一个用于定位的祖先元素,即查找祖先元素中position为relative或absolute的元素。
语法:offsetPosition() 此方法没有参数 由于CSS的绝对定位的定位基准是相对最近的一个已定位元素,因此此方法的作用不言而喻。
$("span").offsetParent(); 23380152bb7a1f6d26abfb41d9f06425 //选中的是p,因此p是已定位元素。 e388a4556c0f65e1904146cc1a846bee 45a2772a6b6107b401db3c9b82c049c2我是一个span54bdf357c58b8a65c66d7c19c8e4d114 94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3
十八、parent() 获取指定元素的直接父元素
语法:parent(expr) expr为筛选条件,如果直接父元素不符合条件,则不返回任何元素(无论它的祖先是否具有能与expr匹配的)
$("span").parent(); 23380152bb7a1f6d26abfb41d9f06425 e388a4556c0f65e1904146cc1a846bee //我是span的直接父元素,我会被匹配到 45a2772a6b6107b401db3c9b82c049c2我是一个span54bdf357c58b8a65c66d7c19c8e4d114 94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3
十九、parents() 获取指定元素的所有祖先元素,一直到6c04bd5ca3fcae76e30b72ad730ca86d36cc49f0c466276486e50c850b7e4956
语法:parents(expr) expr为筛选条件,如果某个祖先元素不符合expr则排除
$("span").parents(); 23380152bb7a1f6d26abfb41d9f06425 //我是span的祖先元素,我也会被匹配到.另外6c04bd5ca3fcae76e30b72ad730ca86d36cc49f0c466276486e50c850b7e4956也会被匹配到 e388a4556c0f65e1904146cc1a846bee //我是span的直接父元素,我会被匹配到 45a2772a6b6107b401db3c9b82c049c2我是一个span54bdf357c58b8a65c66d7c19c8e4d114 94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3
二十、parentsUntil() 获取指定元素的祖先元素,知道参数里能匹配到的为止
语法:parentsUntil(expr) expr为停止参数,一直匹配到expr为止,同时参数的条件是不会被匹配中的。
$("span").parentsUntil("p"); 23380152bb7a1f6d26abfb41d9f06425 //我是span的祖先元素,但是我作为停止条件,我也不会被选中 e388a4556c0f65e1904146cc1a846bee //我是span的直接父元素,我会被选中 45a2772a6b6107b401db3c9b82c049c2我是一个span54bdf357c58b8a65c66d7c19c8e4d114 94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3
二十一、prev() 获取指定元素的前一个兄弟元素
语法:prev(expr) expr:可选。当上一个兄弟元素不符合参数中的条件时,不返回任何元素。
$(".p2").prev(); e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 //我会被选中,我是.p2的前一个元素。 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 d42a450727133d488599a1f95fad5570我是第四个P94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3
二十二、prevAll() 获取指定元素前面的所有兄弟元素
语法:prevAll(expr) expr:可选,排除所有不能够被expr匹配上的元素
$(".p4").prevAll(".p2"); e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,虽然我是.p4前面的兄弟元素,但是我没有class=p2 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,我既是.p4前面的兄弟元素,而且我有class=p2 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,虽然我是.p4前面的兄弟元素,但是我没有class=p2 d42a450727133d488599a1f95fad5570我是第四个P94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3
二十三、prevUntil() 获取指定元素前面的所有兄弟元素,直到参数里的条件能够匹配到的。 注意参数条件本身不会被匹配
语法:prevUntil([expr|ele][,fil]) expr匹配表达式 | DOM元素匹配
$(".p4").prevUntil(".p2"); e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,到p2就停止了 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,我是停止条件,不包括我 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,我在.p2前,递归到我在到.p2 d42a450727133d488599a1f95fad5570我是第四个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,我自己怎么可能是我自己前面的呢? 94b3e26ee717c64999d7867364b1b4a3
/******************** 串联 *******************************/
二十四、siblings() 获取指定元素的兄弟元素,不分前后
语法:siblings(expr); expr为筛选条件,不符合条件的不会选中
$(".p2").siblings(); e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,我是.p2的兄弟元素 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 //不会被选中,我是自己 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,我是.p2的兄弟元素 d42a450727133d488599a1f95fad5570我是第四个P94b3e26ee717c64999d7867364b1b4a3 //会被选中,我是.p2的兄弟元素 94b3e26ee717c64999d7867364b1b4a3
二十五、add() 将选中的元素添加到jQuery对象集合中
add(expr|elements|html|jQueryObject) expr:选择器表达式 | DOM表达式 | Html片段 | jQuery对象,将jQuery对象集合一起方便操作;
$(".p2").add("span").css("background-color","red"); e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 //会变红 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 d42a450727133d488599a1f95fad5570我是第四个P94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3 45a2772a6b6107b401db3c9b82c049c2我是一个span54bdf357c58b8a65c66d7c19c8e4d114 //会变红
二十六、andSelf() 将自身加到选中的jQuery集合中,以方便一次性操作
andSelf() 此方法无参数
$(".p2").nextAll().andSelf().css("background-color","red"); e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 //会变红,这就是andSelf()的效果 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 //会变红 d42a450727133d488599a1f95fad5570我是第四个P94b3e26ee717c64999d7867364b1b4a3 //会变红 94b3e26ee717c64999d7867364b1b4a3
二十七、end() 将改变当前选择器选中的操作回退为上一个状态。
end() 此方法没有参数
$(".p2").next().end().css("background-color","red"); e388a4556c0f65e1904146cc1a846bee e388a4556c0f65e1904146cc1a846bee我是第一个P94b3e26ee717c64999d7867364b1b4a3 25fdd61924ec43f68f6d130ac257deb1我是第二个P94b3e26ee717c64999d7867364b1b4a3 //会变红,这就end()的效果 e388a4556c0f65e1904146cc1a846bee我是第三个P94b3e26ee717c64999d7867364b1b4a3 //不会变红,尽管next()方法之后选中的是这一个,但是由于被end()方法回退了因此是上一个。 d42a450727133d488599a1f95fad5570我是第四个P94b3e26ee717c64999d7867364b1b4a3 94b3e26ee717c64999d7867364b1b4a3
The above is the detailed content of How to filter exclude elements using jQuery to modify properties of specified tags. For more information, please follow other related articles on the PHP Chinese website!