Html CSS选择具有部分id的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13533484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSS select elements with partial id
提问by Fernando Andrade
I have some elements generated with PHP and I would like to know if it is possible to select an element with an incomplete id, example:
我有一些用 PHP 生成的元素,我想知道是否可以选择 id 不完整的元素,例如:
<div class="1" id="as_1"> ... </div>
<div class="2" id="bs_1"> ... </div>
<div class="1" id="as_2"> ... </div>
<div class="2" id="bs_2"> ... </div>
The class is being used to things they have in common, but now I need to select them individually but I don't know the entire id name.
该类已经习惯了它们的共同点,但现在我需要单独选择它们,但我不知道整个 id 名称。
Can I use something like:
我可以使用类似的东西:
#as_{ ... }
#bs_{ ... }
回答by BoltClock
Not with ID selectors since they require complete ID names, but with substring attribute selectors:
不是使用 ID 选择器,因为它们需要完整的 ID 名称,而是使用子字符串属性选择器:
div[id^="as_"]
div[id^="bs_"]
But since your elements have a class
attribute anyway, why not add a common class to each group of elements and select by that class to make things simpler? You should be able to determine the grouping class using PHP as you do to generate the IDs.
但是既然您的元素class
无论如何都有一个属性,为什么不为每组元素添加一个公共类并通过该类进行选择以使事情更简单呢?您应该能够像生成 ID 一样使用 PHP 确定分组类。