search
HomeWeb Front-endFront-end Q&AWhat is attribute selector in jquery

What is attribute selector in jquery

Mar 10, 2023 pm 07:15 PM
javascriptjquery

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.

What is attribute selector in jquery

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.

jQuery attribute selector
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”
[selector1][selector2][selectorN]######Multi-attribute selector (attribute intersection selector)############

jQuery这些属性选择器使得选择器具有通配符的功能,有点正则表达式的感觉。下面我们通过一些简单实例来认识一下。

选取含有class属性的div元素:

$("div[class]")

选取type取值为checkbox的input元素:

$("input[type = &#39;checkbox&#39;]")

选取type取值不是checkbox的input元素:

$("input[type != &#39;checkbox&#39;]")

选取class属性包含nav的div元素(class属性可以包含多个值):

$("div[class *= &#39;nav&#39;]")

选取class属性以nav开头的div元素,例如:

<div class="nav-header"></div>:
$("div[class ^= &#39;nav&#39;]")

选取class属性以nav结尾的div元素,例如:

<div class="first-nav"></div>:
$("div[class $= &#39;nav&#39;]")

选取带有id属性并且class属性是以nav开头的div元素,例如:

<div id="container" class="nav-header"></div>:
$("div[id][class ^=&#39;nav&#39;]")

代码示例

<!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=&#39;three&#39; 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=$(&#39;[id]&#39;);
console.log(a);

选中了示例中所有拥有id属性的元素

What is attribute selector in jquery

[attribute=value]属性值选择器

选择属性值为某个特定值的元素。

var a=$(&#39;[id=one]&#39;);
console.log(a);

选中了示例中id=one的元素

What is attribute selector in jquery

[attribute!=value]非属性值选择器

选择所有属性值不为特定值的元素(包括没有该属性的元素)

var a=$(&#39;[class!=eukaryotes_animal]&#39;);
console.log(a);

除了ul#one.eukaryotes_animal没有选中外,包括它的子元素在内的其他元素均在选择范围内。

What is attribute selector in jquery

[attribute^=value]属性值以某个字符串开头的选择器

var a=$(&#39;[class^=eukaryotes]&#39;);
console.log(a);

What is attribute selector in jquery

[attribute$=value]属性值以某个字符串结尾的选择器

var a=$(&#39;[class$=plant]&#39;);
console.log(a);

What is attribute selector in jquery

[attribute*=value]属性值中包含某个字符串的选择器

var a=$(&#39;[class*=yotes_m]&#39;);
console.log(a);

What is attribute selector in jquery

[selector1][selector2][selectorN] 多属性选择器(属性交集选择器)

var a=$(&#39;[class^=eukaryotes_][id]&#39;);
console.log(a);

What is attribute selector in jquery

更多编程相关知识,请访问:编程学习!!

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
CSS: Is it bad to use ID selector?CSS: Is it bad to use ID selector?May 13, 2025 am 12:14 AM

Using ID selectors is not inherently bad in CSS, but should be used with caution. 1) ID selector is suitable for unique elements or JavaScript hooks. 2) For general styles, class selectors should be used as they are more flexible and maintainable. By balancing the use of ID and class, a more robust and efficient CSS architecture can be implemented.

HTML5: Goals in 2024HTML5: Goals in 2024May 13, 2025 am 12:13 AM

HTML5'sgoalsin2024focusonrefinementandoptimization,notnewfeatures.1)Enhanceperformanceandefficiencythroughoptimizedrendering.2)Improveaccessibilitywithrefinedattributesandelements.3)Addresssecurityconcerns,particularlyXSS,withwiderCSPadoption.4)Ensur

What are the main areas where HTML5 tried to improve?What are the main areas where HTML5 tried to improve?May 13, 2025 am 12:12 AM

HTML5aimedtoimprovewebdevelopmentinfourkeyareas:1)Multimediasupport,2)Semanticstructure,3)Formcapabilities,and4)Offlineandstorageoptions.1)HTML5introducedandelements,simplifyingmediaembeddingandenhancinguserexperience.2)Newsemanticelementslikeandimpr

CSS ID and Class: common mistakesCSS ID and Class: common mistakesMay 13, 2025 am 12:11 AM

IDsshouldbeusedforJavaScripthooks,whileclassesarebetterforstyling.1)Useclassesforstylingtoallowforeasierreuseandavoidspecificityissues.2)UseIDsforJavaScripthookstouniquelyidentifyelements.3)Avoiddeepnestingtokeepselectorssimpleandimproveperformance.4

What is thedifference between class and id selector?What is thedifference between class and id selector?May 12, 2025 am 12:13 AM

Classselectorsareversatileandreusable,whileidselectorsareuniqueandspecific.1)Useclassselectors(denotedby.)forstylingmultipleelementswithsharedcharacteristics.2)Useidselectors(denotedby#)forstylinguniqueelementsonapage.Classselectorsoffermoreflexibili

CSS IDs vs Classes: The real differencesCSS IDs vs Classes: The real differencesMay 12, 2025 am 12:10 AM

IDsareuniqueidentifiersforsingleelements,whileclassesstylemultipleelements.1)UseIDsforuniqueelementsandJavaScripthooks.2)Useclassesforreusable,flexiblestylingacrossmultipleelements.

CSS: What if I use just classes?CSS: What if I use just classes?May 12, 2025 am 12:09 AM

Using a class-only selector can improve code reusability and maintainability, but requires managing class names and priorities. 1. Improve reusability and flexibility, 2. Combining multiple classes to create complex styles, 3. It may lead to lengthy class names and priorities, 4. The performance impact is small, 5. Follow best practices such as concise naming and usage conventions.

ID and Class Selectors in CSS: A Beginner's GuideID and Class Selectors in CSS: A Beginner's GuideMay 12, 2025 am 12:06 AM

ID and class selectors are used in CSS for unique and multi-element style settings respectively. 1. The ID selector (#) is suitable for a single element, such as a specific navigation menu. 2.Class selector (.) is used for multiple elements, such as unified button style. IDs should be used with caution, avoid excessive specificity, and prioritize class for improved style reusability and flexibility.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft