jQuery or CSS selector to select all IDs that start with some string(jQuery 或 CSS 选择器选择所有以某个字符串开头的 ID)
问题描述
如何选择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 选择器 # 选择 ID,但对于更复杂的匹配,您可以使用 attribute-starts-with 选择器(作为 jQuery 选择器,或作为 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 添加一个类,然后定位该类.无论如何,您将失去 ID 选择器提供的额外特异性,因为属性选择器与类选择器具有相同的特异性.另外,只使用一个类就可以让事情变得更简单.
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.
这篇关于jQuery 或 CSS 选择器选择所有以某个字符串开头的 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:jQuery 或 CSS 选择器选择所有以某个字符串开头的
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
