What is attribute selector in jquery
In jquery, the attribute selector is a selector based on element attributes as filter conditions, which refers to a way to select elements through "element attributes"; this selector can find elements with specific attributes or Elements with specific attribute values, that is, you can match HTML elements through existing attribute names or attribute values, and then operate on HTML elements with specified attributes. The jQuery attribute selector makes the selector function like a wildcard, a bit like a regular expression.
The operating environment of this tutorial: windows7 system, jquery3.6 version, Dell G3 computer.
jquery attribute selector introduction
jQuery attribute selector is a selector based on element attributes as filter conditions.
Attribute selector refers to a way to select elements through "element attributes". Attribute selectors can find elements with specific attributes or specific attribute values, that is, they can match HTML elements through existing attribute names or attribute values, and then operate on HTML elements with specified attributes. We all know what the attributes of the
element are. The id, type, and value in the code below are the attributes of the input element.
<input id="btn" type="button" value="按钮" />
In jQuery, common attribute selectors are shown in the table. Where E refers to the element, attr refers to the attribute (attr), and value refers to the attribute value.
Selector | Description |
---|---|
E[attr] | Select element E, where the E element must have the attr attribute |
E[attr = “value”] | Select element E, where the value of the attr attribute of element E is value |
E[attr!= “value”] | Select element E, where the value of attr of element E is value |
E[attr!= “value”] | The attribute value is not value |
E[attr ^= “value”] | Select element E, where the attr attribute value of E element starts with “value” Any character of |
E[attr $="value"] | selects element E, where the value of the attr attribute of the E element is anything ending with "value" Characters |
E[attr *= “value”] | Select element E, where the value of the attr attribute of the E element is any character containing “value” |
E[attr |= “value”] | Select element E, where the attr attribute value of E element is equal to “value” or starts with “value” |
E[attr ~= “value”] | Select element E, where the attr attribute value of E element is equal to “value” or contains “value” |
jQuery这些属性选择器使得选择器具有通配符的功能,有点正则表达式的感觉。下面我们通过一些简单实例来认识一下。
选取含有class属性的div元素:
$("div[class]")
选取type取值为checkbox的input元素:
$("input[type = 'checkbox']")
选取type取值不是checkbox的input元素:
$("input[type != 'checkbox']")
选取class属性包含nav的div元素(class属性可以包含多个值):
$("div[class *= 'nav']")
选取class属性以nav开头的div元素,例如:
<div class="nav-header"></div>: $("div[class ^= 'nav']")
选取class属性以nav结尾的div元素,例如:
<div class="first-nav"></div>: $("div[class $= 'nav']")
选取带有id属性并且class属性是以nav开头的div元素,例如:
<div id="container" class="nav-header"></div>: $("div[id][class ^='nav']")
代码示例
<!DOCTYPE style="color:rgb(73 238 255)">html> <style="color:rgb(73 238 255)">html> <style="color:rgb(73 238 255)">head lang="style="color:rgb(255 95 0)">zh-CN"> <style="color:rgb(73 238 255)">meta charset="style="color:rgb(255 95 0)">UTF-8"> <style="color:rgb(73 238 255)">meta name="style="color:rgb(98 189 255)">viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"> <style="color:rgb(255 95 0)">title>多项选择器</style="color:rgb(255 95 0)">title> <style></style> </style="color:rgb(73 238 255)">head> <body> <section> <ul id="style="color:rgb(255 111 119)">one" class="style="color:rgb(98 189 255)">eukaryotes_animal"> <li>猴子</li> <li>猛犸</li> <li>猩猩</li> </ul> <ul id="style="color:rgb(255 111 119)">two" class="style="color:rgb(98 189 255)">eukaryotes_plant"> <li>牡丹</li> <li>樱花</li> <li>仙人掌</li> </ul> <ul id='three' class="style="color:rgb(98 189 255)">prokaryotes_microbe"> <li>细菌</li> <li>蓝细菌</li> <li>放线菌</li> <li>支原体</li> </ul> </section> <script color:rgb(255 95 0)">https://style="color:rgb(255 111 119)">cdn.style="color:rgb(253 97 106)">bootcss.com/style="color:rgb(255 211 0)">jquery/3.3.1/style="color:rgb(255 211 0)">jquery.js"></script> <script type="text/javascript"> $(document).ready(function () { //此处填写代码 }); </script> </body> </style="color:rgb(73 238 255)">html>
[attribute] 属性名选择器
选择拥有该属性名的元素。
var a=$('[id]'); console.log(a);
选中了示例中所有拥有id属性的元素
[attribute=value]属性值选择器
选择属性值为某个特定值的元素。
var a=$('[id=one]'); console.log(a);
选中了示例中id=one的元素
[attribute!=value]非属性值选择器
选择所有属性值不为特定值的元素(包括没有该属性的元素)
var a=$('[class!=eukaryotes_animal]'); console.log(a);
除了ul#one.eukaryotes_animal没有选中外,包括它的子元素在内的其他元素均在选择范围内。
[attribute^=value]属性值以某个字符串开头的选择器
var a=$('[class^=eukaryotes]'); console.log(a);
[attribute$=value]属性值以某个字符串结尾的选择器
var a=$('[class$=plant]'); console.log(a);
[attribute*=value]属性值中包含某个字符串的选择器
var a=$('[class*=yotes_m]'); console.log(a);
[selector1][selector2][selectorN] 多属性选择器(属性交集选择器)
var a=$('[class^=eukaryotes_][id]'); console.log(a);
更多编程相关知识,请访问:编程学习!!
The above is the detailed content of What is attribute selector in jquery. 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

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

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:
