jQuery 或 CSS 选择器选择以某个字符串开头的所有 ID
如何选择 id
以“player_”开头的所有元素?
我有多个这样的元素:
<div id="player_290x3dfda">text</div>
每个 id
上都有一个唯一的标记。如何使用 jQuery 或纯 CSS 选择所有这些元素?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常,您会使用 ID 选择器
#
选择 ID,但对于更复杂的匹配,您可以使用 attribute-starts-with 选择器(作为 jQuery 选择器,或作为 CSS3 选择器):但是,如果您能够修改该 HTML,则应该向播放器
div
然后定位该类。无论如何,您都会失去 ID 选择器提供的额外特异性,因为属性选择器与类选择器具有相同的特异性。另外,仅仅使用一个类就可以让事情变得更加简单。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):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.试试这个:
try this:
您可以使用元字符,例如
*
(http://api.jquery.com /类别/选择器/)。所以我认为你可以使用
$('#player_*')
。在您的情况下,您还可以尝试“属性开头为”选择器:
http://api.jquery.com/attribute-starts-with-selector/:
$('div[id^="player_"]')
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_"]')
这对我有用..选择所有以“players_”关键字开头的 Div 并显示它。
This worked for me..select all Div starts with "players_" keyword and display it.