attr as property in css selector [duplicate]

[亡魂溺海] 提交于 2019-12-02 04:26:22

Try this code:

 div[data-weight="bold"]{
      font-weight: bold;
    }

http://www.w3.org/TR/selectors/#attribute-selectors

JSFIDDLE

You can use the following:

div[data-weight="bold"] { font-weight: bold; }

But I don't believe there's any way to use an attribute as a property without some JavaScript

You cannot achieve what you ask about in pure CSS. Using jQuery it could be done this way:

$.each($('div'), function(i, div) {
    $.each($(this).data(), function(key, val) {
        var realKey = key.replace(/([A-Z]{1})/g, '-$1').toLowerCase();
        $(div).css(realKey, val);
    })
})​

Check this DEMO.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!