jQuery logojQuery API 中文文档 | jQuery 中文网

  • 首页
  • 浏览器支持
  • 英文文档镜像

分类:Attribute


The CSS specification allows elements to be identified by their attributes. While not supported by some older browsers for the purpose of styling documents, jQuery allows you to employ them regardless of the browser being used.

When using any of the following attribute selectors, you should account for attributes that have multiple, space-separated values. Since these selectors see attribute values as a single string, this selector, for example, $("a[rel='nofollow']"), will select <a href="example.html" rel="nofollow">Some text</a> but not <a href="example.html" rel="nofollow foe">Some text</a>.

Attribute values in selector expressions must follow the rules for W3C CSS selectors; in general, that means anything other than a valid identifier should be surrounded by quotation marks.

  • double quotes inside single quotes: $('a[rel="nofollow self"]')
  • single quotes inside double quotes: $("a[rel='nofollow self']")
  • escaped single quotes inside single quotes: $('a[rel=\'nofollow self\']')
  • escaped double quotes inside double quotes: $("a[rel=\"nofollow self\"]")

The variation you choose is generally a matter of style or convenience.

Note: In jQuery 1.3 [@attr] style selectors were removed (they were previously deprecated in jQuery 1.2). Simply remove the "@" symbol from your selectors in order to make them work again.

Selectors > Attribute

Attribute Contains Prefix Selector [name|="value"]

选择指定属性值等于给定字符串或以该字符串为前缀(该字符串后跟一个连字符“-” )的元素。
Selectors > Attribute

Attribute Contains Selector [name*="value"]

选择指定属性具有包含一个给定的子字符串的元素。(选择给定的属性是以包含某些值的元素)
Selectors > Attribute

Attribute Contains Word Selector [name~="value"]

选择指定属性用空格分隔的值中包含一个给定值的元素。
Selectors > Attribute

Attribute Ends With Selector [name$="value"]

选择指定属性是以给定值结尾的元素。这个比较是区分大小写的。
Selectors > Attribute

Attribute Equals Selector [name="value"]

选择指定属性是给定值的元素。
Selectors > Attribute | Selectors > jQuery Extensions

Attribute Not Equal Selector [name!="value"]

选择不存在指定属性,或者指定的属性值不等于给定值的元素。
Selectors > Attribute

Attribute Starts With Selector [name^="value"]

选择指定属性是以给定字符串开始的元素
Selectors > Attribute

Has Attribute Selector [name]

选择所有具有指定属性的元素,该属性可以是任何值。
Selectors > Attribute

Multiple Attribute Selector [name="value"][name2="value2"]

选择匹配所有指定的属性筛选器的元素
  • Ajax
    • Global Ajax Event Handlers
    • Helper Functions
    • Low-Level Interface
    • Shorthand Methods
  • Attributes
  • Callbacks Object
  • Core
  • CSS
  • Data
  • Deferred Object
  • Deprecated
    • Deprecated 1.3
    • Deprecated 1.4
    • Deprecated 1.7
    • Deprecated 1.8
    • Deprecated 1.9
    • Deprecated 1.10
    • Deprecated 3.0
    • Deprecated 3.2
    • Deprecated 3.3
    • Deprecated 3.4
    • Deprecated 3.5
  • Dimensions
  • Effects
    • Basics
    • Custom
    • Fading
    • Sliding
  • Events
    • Browser Events
    • Document Loading
    • Event Handler Attachment
    • Event Object
    • Form Events
    • Keyboard Events
    • Mouse Events
  • Forms
  • Internals
  • Manipulation
    • Class Attribute
    • Copying
    • DOM Insertion
    • DOM Insertion, Around
    • DOM Insertion, Inside
    • DOM Insertion, Outside
    • DOM Removal
    • DOM Replacement
    • General Attributes
    • Style Properties
  • Miscellaneous
    • Collection Manipulation
    • Data Storage
    • DOM Element Methods
    • Setup Methods
  • Offset
  • Properties
    • Properties of jQuery Object Instances
    • Properties of the Global jQuery Object
  • Removed
  • Selectors
    • Attribute
    • Basic
    • Basic Filter
    • Child Filter
    • Content Filter
    • Form
    • Hierarchy
    • jQuery Extensions
    • Visibility Filter
  • Traversing
    • Filtering
    • Miscellaneous Traversing
    • Tree Traversal
  • Uncategorized
  • Utilities
  • Version
    • Version 1.0
    • Version 1.0.4
    • Version 1.1
    • Version 1.1.2
    • Version 1.1.3
    • Version 1.1.4
    • Version 1.2
    • Version 1.2.3
    • Version 1.2.6
    • Version 1.3
    • Version 1.4
    • Version 1.4.1
    • Version 1.4.2
    • Version 1.4.3
    • Version 1.4.4
    • Version 1.5
    • Version 1.5.1
    • Version 1.6
    • Version 1.7
    • Version 1.8
    • Version 1.9
    • Version 1.10 & 2.0
    • Version 1.11 & 2.1
    • Version 1.12 & 2.2
    • Version 3.0
    • Version 3.1
    • Version 3.2
    • Version 3.3
    • Version 3.4
    • Version 3.5
    • Version 3.6
    • Version 3.7
    • All
  • Bootstrap
  • TailwindCSS
  • React
  • StyleX
  • Next.js
  • Nextra
  • Docusaurus
  • Lodash
  • NPM
  • Yarn
  • pnpm
  • Webpack
  • Rollup
  • Parcle
  • esbuild
  • Biome
  • SASS
  • Gulp
  • Markdonw
  • Svelte
  • Alpine.js
  • Prettier
  • Axios
  • Deno
  • Bun
  • Eleventy(11ty)
  • Strapi
  • Cheerio
  • Astro

Copyright 2024 jQuery 中文网. 冀ICP备14002930号-6