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
React: The Foundation for Modern Frontend DevelopmentReact: The Foundation for Modern Frontend DevelopmentApr 19, 2025 am 12:23 AM

React is a JavaScript library for building modern front-end applications. 1. It uses componentized and virtual DOM to optimize performance. 2. Components use JSX to define, state and attributes to manage data. 3. Hooks simplify life cycle management. 4. Use ContextAPI to manage global status. 5. Common errors require debugging status updates and life cycles. 6. Optimization techniques include Memoization, code splitting and virtual scrolling.

The Future of React: Trends and Innovations in Web DevelopmentThe Future of React: Trends and Innovations in Web DevelopmentApr 19, 2025 am 12:22 AM

React's future will focus on the ultimate in component development, performance optimization and deep integration with other technology stacks. 1) React will further simplify the creation and management of components and promote the ultimate in component development. 2) Performance optimization will become the focus, especially in large applications. 3) React will be deeply integrated with technologies such as GraphQL and TypeScript to improve the development experience.

React: A Powerful Tool for Building UI ComponentsReact: A Powerful Tool for Building UI ComponentsApr 19, 2025 am 12:22 AM

React is a JavaScript library for building user interfaces. Its core idea is to build UI through componentization. 1. Components are the basic unit of React, encapsulating UI logic and styles. 2. Virtual DOM and state management are the key to component work, and state is updated through setState. 3. The life cycle includes three stages: mount, update and uninstall. The performance can be optimized using reasonably. 4. Use useState and ContextAPI to manage state, improve component reusability and global state management. 5. Common errors include improper status updates and performance issues, which can be debugged through ReactDevTools. 6. Performance optimization suggestions include using memo, avoiding unnecessary re-rendering, and using us

Using React with HTML: Rendering Components and DataUsing React with HTML: Rendering Components and DataApr 19, 2025 am 12:19 AM

Using HTML to render components and data in React can be achieved through the following steps: Using JSX syntax: React uses JSX syntax to embed HTML structures into JavaScript code, and operates the DOM after compilation. Components are combined with HTML: React components pass data through props and dynamically generate HTML content, such as. Data flow management: React's data flow is one-way, passed from the parent component to the child component, ensuring that the data flow is controllable, such as App components passing name to Greeting. Basic usage example: Use map function to render a list, you need to add a key attribute, such as rendering a fruit list. Advanced usage example: Use the useState hook to manage state and implement dynamics

React's Purpose: Building Single-Page Applications (SPAs)React's Purpose: Building Single-Page Applications (SPAs)Apr 19, 2025 am 12:06 AM

React is the preferred tool for building single-page applications (SPAs) because it provides efficient and flexible ways to build user interfaces. 1) Component development: Split complex UI into independent and reusable parts to improve maintainability and reusability. 2) Virtual DOM: Optimize rendering performance by comparing the differences between virtual DOM and actual DOM. 3) State management: manage data flow through state and attributes to ensure data consistency and predictability.

React: The Power of a JavaScript Library for Web DevelopmentReact: The Power of a JavaScript Library for Web DevelopmentApr 18, 2025 am 12:25 AM

React is a JavaScript library developed by Meta for building user interfaces, with its core being component development and virtual DOM technology. 1. Component and state management: React manages state through components (functions or classes) and Hooks (such as useState), improving code reusability and maintenance. 2. Virtual DOM and performance optimization: Through virtual DOM, React efficiently updates the real DOM to improve performance. 3. Life cycle and Hooks: Hooks (such as useEffect) allow function components to manage life cycles and perform side-effect operations. 4. Usage example: From basic HelloWorld components to advanced global state management (useContext and

React's Ecosystem: Libraries, Tools, and Best PracticesReact's Ecosystem: Libraries, Tools, and Best PracticesApr 18, 2025 am 12:23 AM

The React ecosystem includes state management libraries (such as Redux), routing libraries (such as ReactRouter), UI component libraries (such as Material-UI), testing tools (such as Jest), and building tools (such as Webpack). These tools work together to help developers develop and maintain applications efficiently, improve code quality and development efficiency.

React and Frontend Development: A Comprehensive OverviewReact and Frontend Development: A Comprehensive OverviewApr 18, 2025 am 12:23 AM

React is a JavaScript library developed by Facebook for building user interfaces. 1. It adopts componentized and virtual DOM technology to improve the efficiency and performance of UI development. 2. The core concepts of React include componentization, state management (such as useState and useEffect) and the working principle of virtual DOM. 3. In practical applications, React supports from basic component rendering to advanced asynchronous data processing. 4. Common errors such as forgetting to add key attributes or incorrect status updates can be debugged through ReactDevTools and logs. 5. Performance optimization and best practices include using React.memo, code segmentation and keeping code readable and maintaining dependability

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 Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)