CSS选择带有部分ID的元素 [英] CSS select elements with partial id

查看:182
本文介绍了CSS选择带有部分ID的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些用PHP生成的元素,我想知道是否可以选择一个具有不完整ID的元素,例如:

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:

<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>

类被用于他们共同的东西,但现在我需要单独选择它们,我不知道整个id的名称。

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.

我可以使用像:

#as_{ ... }
#bs_{ ... }

$ b

推荐答案

不使用ID选择器,因为它们需要完整的ID名称,但使用子串属性选择器

Not with ID selectors since they require complete ID names, but with substring attribute selectors:

div[id^="as_"]
div[id^="bs_"]

但是由于你的元素有一个 class 属性,为什么不添加一个公共类到每组元素,选择该类使事情更简单?您应该能够使用PHP确定分组类,就像生成ID一样。

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.

这篇关于CSS选择带有部分ID的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆