开发者

Jquery select all objects with a particular attribute

开发者 https://www.devze.com 2023-01-30 23:25 出处:网络
I want to select all elements on my page that have a particular attribute. For example, I have an attribute called \"tip\" I add tip=\"...whatever...\" to a lot of my html tags. I would like to be ab

I want to select all elements on my page that have a particular attribute.

For example, I have an attribute called "tip" I add tip="...whatever..." to a lot of my html tags. I would like to be able to select all html t开发者_StackOverflow中文版ags that have this attribute.

NOT all html tags that have that attribute equal to a particular value. Just have the attribute AT ALL; REGARDLESS of value.

I apologize for my confusing wording and inadiquite vocabulary in my question. I have now updated the question to use proper wording.

Thanks!


$( '[tip]' )

attributeHas selector

"Description: Selects elements that have the specified attribute, with any value."

See http://api.jquery.com/has-attribute-selector/


Update:


You can do like this:

$('[tip="something"]')

You can replace something with whatever your string is.


You can use :contains filter selector like this:

$('p:contains("tip")').color('red', 'green');

That for example will select all paragraphs that have tip keyword in their value/html.


If they are all attributes - e.g. tip="foo" then you can use the Attribute Equals Selector as shown below:

jQuery('[attribute="value"]')

So in your case:

$('div[tip="foo"]')

Demo Here

0

精彩评论

暂无评论...
验证码 换一张
取 消