开发者

select some element by ID in jQuery [duplicate]

开发者 https://www.devze.com 2023-04-02 20:26 出处:网络
This question already has answers here: Closed 11开发者_运维知识库 years ago. Possible Duplicate:
This question already has answers here: Closed 11开发者_运维知识库 years ago.

Possible Duplicate:

Find all elements on a page whose element ID contains a certain text using jQuery

I need to select all elements have "user" word in their ID.

in attribute I will do it by $= but how can I do it by element ID?


You want something like this

$('[id*="user"]')

selects all elements

[id with an id

*="user" that contains user

Example: http://jsfiddle.net/jasongennaro/QXGen/2/

Here is more info on the attribute contains selector: http://api.jquery.com/attribute-contains-selector/


You'd do it with the use of the *= selector:

$('[id*="user"]').doStuff();


Use the contains variant of the named attribute selector with the id attribute.

$('[id*="user"]')...


An attribute selector like $('[id*="user"]') will match elements with the id 'user', 'users', 'superuser', 'abusers', 'user-details', etc.

If you had a more rigid structure for your IDs and wanted to only match things like 'user' and 'super-user' etc., you could try something like:

var userWordTest = RegExp("\\b" + "user" + "\\b");
$('[id]').filter(function() { return userWordTest.test(this.id); })
0

精彩评论

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