jQuery or CSS selector to select all IDs that start with some string [duplicate]

*爱你&永不变心* 提交于 2019-12-17 02:12:06

问题


How can I select all elements whose id starts with "player_"?

I have multiple elements like this:

<div id="player_290x3dfda">text</div>

Every id has a unique stamp on it. How can I select all those elements, either with jQuery or with pure CSS?


回答1:


Normally you would select IDs using the ID selector #, but for more complex matches you can use the attribute-starts-with selector (as a jQuery selector, or as a CSS3 selector):

div[id^="player_"]

If you are able to modify that HTML, however, you should add a class to your player divs then target that class. You'll lose the additional specificity offered by ID selectors anyway, as attribute selectors share the same specificity as class selectors. Plus, just using a class makes things much simpler.




回答2:


try this:

$('div[id^="player_"]')



回答3:


You can use meta characters like * (http://api.jquery.com/category/selectors/). So I think you just can use $('#player_*').

In your case you could also try the "Attribute starts with" selector: http://api.jquery.com/attribute-starts-with-selector/: $('div[id^="player_"]')




回答4:


$('div[id ^= "player_"]');

This worked for me..select all Div starts with "players_" keyword and display it.



来源:https://stackoverflow.com/questions/5002966/jquery-or-css-selector-to-select-all-ids-that-start-with-some-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!