“动态” CSS id名称?如何将它们添加到CSS文件? [英] "Dynamic" CSS id names? How to add them to the CSS file?

查看:512
本文介绍了“动态” CSS id名称?如何将它们添加到CSS文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态生成的应用程序中的DIV ID,最后有ID。例如:< div id =vote_score_72> < div id =vote_score_73> < div id =vote_score_74> 等等。

I have DIV ids in an application that are generated dynamically, with an ID at the end. For example: <div id="vote_score_72">, <div id="vote_score_73">, <div id="vote_score_74"> and so on.

如何在CSS文件中编写该id,以便可以对其应用样式?

How can I write that id in the CSS file so I can apply a style to it?

推荐答案

除了建议您使用类来实现此功能(和您应该),您可以 value-begin-with selector(这可能不是正式名称):

Further to the suggestion that you should use classes for this functionality (and you should), you can, instead, use the attribute-value-begins-with selector (this may not be the formal name):

div[id^=vote_score] {
    color: #f00;
    /* and so on... */
}

JS Fiddle demo



已编辑,以添加指向 Quirksmode.org 高级属性选择器的兼容性表

参考文献:

  • CSS3 Attribute-selectors, at Sitepoint.com.
  • Attribute selectors, at CSS3.info
  • Attribute Selectors, at the Mozilla Developers Network.

这篇关于“动态” CSS id名称?如何将它们添加到CSS文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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