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

查看:17
本文介绍了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.

我可以使用类似的东西吗:

Can I use something like:

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

推荐答案

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

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

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

但是既然你的元素有一个 class 属性,为什么不给每组元素添加一个公共类并通过该类进行选择以使事情变得更简单呢?您应该能够像生成 ID 一样使用 PHP 确定分组类.

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天全站免登陆