


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
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会被选中 <p> <p>我是第一个P</p> <p>我是第二个P</p> //会被选中,索引值为1 <p>我是第三个P</p> <p>我是第四个P</p> </p>
Syntax: first() This method has no parameters
$("p").first(); <p> <p>我是第一个P</p> //我的索引值是0,我是第一个,我会被选中 <p>我是第二个P</p> <p>我是第三个P</p> <p>我是第四个P</p> </p>
Syntax: last() This method has no parameters
$("p").last(); <p> <p>我是第一个P</p> <p>我是第二个P</p> <p>我是第三个P</p> <p>我是第四个P</p> //我是最后一个,我会被选中 </p>
Syntax: hasClass(class) Class is the category name //returns a Boolean value
if($("p").hasClass("p2")) { alert("我里面含有class=p2的元素"); //会弹出,p里的确存在class="p2"的元素 } <p> <p>我是第一个P</p> <p class="p2">我是第二个P</p> <p>我是第三个P</p> <p>我是第四个P</p> </p>
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"); <p> <p>我是第一个P</p> <p class="p2">我是第二个P</p> //我会被选中,我的class="p2" <p>我是第三个P</p> <p>我是第四个P</p> </p>
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"); } <p> <p>我是第一个P</p> <p class="p2">我是第二个P</p> //我会被选中,我的class="p2" <p>我是第三个P</p> <p>我是第四个P</p> </p>
7. map ()
8. has() Filter out elements containing specified sub-elements
Syntax: has(expr|ele) expr: selection expression | DOM element selection
$("p").has("p"); <p> //本p会被选中,因为该p含有p子元素 <p>我是第一个P</p> <p class="p2">我是第二个P</p> <p>我是第三个P</p> <p>我是第四个P</p> </p> <p> <span>我是一个span</spam> </p>
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"); <p> <p>我是第一个P</p> //会被选中,没有class=p2 <p class="p2">我是第二个P</p> //不会被选中,因为有class=p2被not(".p2")排除了 <p>我是第三个P</p> //会被选中,没有class=p2 <p>我是第四个P</p> //会被选中,没有class=p2 </p>
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) <p> <p>我是第一个P</p> //不会被选中,我索引为0 <p class="p2">我是第二个P</p> //会被选中,我索引为1 <p>我是第三个P</p> //会被选中,我索引为2 <p>我是第四个P</p> //不会被选中,虽然我的索引为3,但是不包括我 </p>
************************ 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"); <p> <p>我是第一个P</p> //不会被选中,虽然我是p的子元素,但是我没class=p2 <p class="p2">我是第二个P</p> //会被选中,我既是p的子元素,又有class=p2 <p>我是第三个P</p> //不会被选中,虽然我是p的子元素,但是我没class=p2 <p>我是第四个P</p> //不会被选中,虽然我是p的子元素,但是我没class=p2 </p>
12. closest() Starting from the current element, return the first matching parent element that meets the conditions
$("span").closest("p","p"); <p> //不会被选中,被P抢了先机 <p>我是第一个P //P会被选中,因为P符合条件,而且是最先匹配到的,虽然p也符合条件了,但是p不是最先匹配到的。因此p不会被选中。 <span>我是P里的span</span> </p> </p>
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"); <p> <p>我是第一个P</p> //不会被选中,虽然我是p的子元素,但是我没class=p2 <p class="p2">我是第二个P</p> //会被选中,我既是p的子元素,又有class=p2 <p>我是第三个P</p> //不会被选中,虽然我是p的子元素,但是我没class=p2 <p>我是第四个P</p> //不会被选中,虽然我是p的子元素,但是我没class=p2 </p>
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的下一个。 <p> <p>我是第一个P</p> <p class="p2">我是第二个P</p> <p>我是第三个P</p> //我是.p2的next <p class="p4">我是第四个P</p> </p>
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会被选中 <p> <p>我是第一个P</p> <p class="p2">我是第二个P</p> <p>我是第三个P</p> //会被选中,是.p2后面的兄弟元素 <p class="p4">我是第四个P</p> //会被选中,是.p2后面的兄弟元素 </p>
16. nextUntil() Get the subsequent elements until the parameters match, excluding the end condition
语法:nextUntil([expr|ele][,fil]) expr筛选表达式 | DOM元素筛选,注意不包括参数里的那一个
$(".p2").nextUntil(".p4"); //注意此方法并不会包括.p4 <p> <p>我是第一个P</p> <p class="p2">我是第二个P</p> <p>我是第三个P</p> //会被选中,是.p2后面的兄弟元素 <p class="p4">我是第四个P</p> //不会被选中,我作为结束条件,但不包括我 </p>
十七、offsetPosition() 返回第一个用于定位的祖先元素,即查找祖先元素中position为relative或absolute的元素。
语法:offsetPosition() 此方法没有参数 由于CSS的绝对定位的定位基准是相对最近的一个已定位元素,因此此方法的作用不言而喻。
$("span").offsetParent(); <p style="position:relative"> //选中的是p,因此p是已定位元素。 <p> <span>我是一个span</span> </p> </p>
十八、parent() 获取指定元素的直接父元素
语法:parent(expr) expr为筛选条件,如果直接父元素不符合条件,则不返回任何元素(无论它的祖先是否具有能与expr匹配的)
$("span").parent(); <p style="position:relative"> <p> //我是span的直接父元素,我会被匹配到 <span>我是一个span</span> </p> </p>
十九、parents() 获取指定元素的所有祖先元素,一直到
语法:parents(expr) expr为筛选条件,如果某个祖先元素不符合expr则排除
$("span").parents(); <p style="position:relative"> //我是span的祖先元素,我也会被匹配到.另外<body></body>也会被匹配到 <p> //我是span的直接父元素,我会被匹配到 <span>我是一个span</span> </p> </p>
二十、parentsUntil() 获取指定元素的祖先元素,知道参数里能匹配到的为止
语法:parentsUntil(expr) expr为停止参数,一直匹配到expr为止,同时参数的条件是不会被匹配中的。
$("span").parentsUntil("p"); <p style="position:relative"> //我是span的祖先元素,但是我作为停止条件,我也不会被选中 <p> //我是span的直接父元素,我会被选中 <span>我是一个span</span> </p> </p>
二十一、prev() 获取指定元素的前一个兄弟元素
语法:prev(expr) expr:可选。当上一个兄弟元素不符合参数中的条件时,不返回任何元素。
$(".p2").prev(); <p> <p>我是第一个P</p> //我会被选中,我是.p2的前一个元素。 <p class="p2">我是第二个P</p> <p>我是第三个P</p> <p class="p4">我是第四个P</p> </p>
二十二、prevAll() 获取指定元素前面的所有兄弟元素
语法:prevAll(expr) expr:可选,排除所有不能够被expr匹配上的元素
$(".p4").prevAll(".p2"); <p> <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> </p>
二十三、prevUntil() 获取指定元素前面的所有兄弟元素,直到参数里的条件能够匹配到的。 注意参数条件本身不会被匹配
语法:prevUntil([expr|ele][,fil]) expr匹配表达式 | DOM元素匹配
$(".p4").prevUntil(".p2"); <p> <p>我是第一个P</p> //不会被选中,到p2就停止了 <p class="p2">我是第二个P</p> //不会被选中,我是停止条件,不包括我 <p>我是第三个P</p> //会被选中,我在.p2前,递归到我在到.p2 <p class="p4">我是第四个P</p> //不会被选中,我自己怎么可能是我自己前面的呢? </p>
/******************** 串联 *******************************/
二十四、siblings() 获取指定元素的兄弟元素,不分前后
语法:siblings(expr); expr为筛选条件,不符合条件的不会选中
$(".p2").siblings(); <p> <p>我是第一个P</p> //会被选中,我是.p2的兄弟元素 <p class="p2">我是第二个P</p> //不会被选中,我是自己 <p>我是第三个P</p> //会被选中,我是.p2的兄弟元素 <p class="p4">我是第四个P</p> //会被选中,我是.p2的兄弟元素 </p>
二十五、add() 将选中的元素添加到jQuery对象集合中
add(expr|elements|html|jQueryObject) expr:选择器表达式 | DOM表达式 | Html片段 | jQuery对象,将jQuery对象集合一起方便操作;
$(".p2").add("span").css("background-color","red"); <p> <p>我是第一个P</p> <p class="p2">我是第二个P</p> //会变红 <p>我是第三个P</p> <p class="p4">我是第四个P</p> </p> <span>我是一个span</span> //会变红
二十六、andSelf() 将自身加到选中的jQuery集合中,以方便一次性操作
andSelf() 此方法无参数
$(".p2").nextAll().andSelf().css("background-color","red"); <p> <p>我是第一个P</p> <p class="p2">我是第二个P</p> //会变红,这就是andSelf()的效果 <p>我是第三个P</p> //会变红 <p class="p4">我是第四个P</p> //会变红 </p>
二十七、end() 将改变当前选择器选中的操作回退为上一个状态。
end() 此方法没有参数
$(".p2").next().end().css("background-color","red"); <p> <p>我是第一个P</p> <p class="p2">我是第二个P</p> //会变红,这就end()的效果 <p>我是第三个P</p> //不会变红,尽管next()方法之后选中的是这一个,但是由于被end()方法回退了因此是上一个。 <p class="p4">我是第四个P</p> </p>
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!

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

Excel is often used to process data in daily office work, and it is often necessary to use the "filter" function. When we choose to perform "filtering" in Excel, we can only filter up to two conditions for the same column. So, do you know how to filter more than 3 keywords at the same time in Excel? Next, let me demonstrate it to you. The first method is to gradually add the conditions to the filter. If you want to filter out three qualifying details at the same time, you first need to filter out one of them step by step. At the beginning, you can first filter out employees with the surname "Wang" based on the conditions. Then click [OK], and then check [Add current selection to filter] in the filter results. The steps are as follows. Similarly, perform filtering separately again

Excel is a frequently used office software. Many users record various data in the table, but the table clearly contains data and is blank when filtering. Regarding this problem, many users don’t know how to solve it. It doesn’t matter. , the content of this software tutorial is to provide answers to the majority of users. Users in need are welcome to check out the solutions. What should I do if there is data in the Excel table but the blanks are filtered? The first reason is that the table contains blank rows. We want to filter all people with the surname "Li", but we can see that the correct results are not filtered out because the table contains blank rows. How to deal with this situation? Solution: Step 1: Select all content and then filter. Press c

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Apple rolled out the iOS 17.4 update on Tuesday, bringing a slew of new features and fixes to iPhones. The update includes new emojis, and EU users will also be able to download them from other app stores. In addition, the update also strengthens the control of iPhone security and introduces more "Stolen Device Protection" setting options to provide users with more choices and protection. "iOS17.3 introduces the "Stolen Device Protection" function for the first time, adding extra security to users' sensitive information. When the user is away from home and other familiar places, this function requires the user to enter biometric information for the first time, and after one hour You must enter information again to access and change certain data, such as changing your Apple ID password or turning off stolen device protection.
