jQuery或CSS选择器来选择以一些字符串开头的所有ID [英] jQuery or CSS selector to select all IDs that start with some string
问题描述
如何选择 id
以player _开头的所有元素?
How can I select all elements whose id
starts with "player_"?
这:
<div id="player_290x3dfda">text</div>
每个 id
。如何使用jQuery或纯CSS选择所有这些元素?
Every id
has a unique stamp on it. How can I select all those elements, either with jQuery or with pure CSS?
推荐答案
通常,您将使用ID选择器#
,但对于更复杂的匹配,您可以使用attribute-starts-with选择器(作为 jQuery selector ,或者作为CSS3选择器):
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_"]
但是,如果您能够修改该HTML,您的播放器 div
s然后定位该类。你会失去ID选择器提供的额外的特异性,因为属性选择器与类选择器具有相同的特异性。此外,只需使用一个类就可以使事情变得更简单。
If you are able to modify that HTML, however, you should add a class to your player div
s 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.
这篇关于jQuery或CSS选择器来选择以一些字符串开头的所有ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!