attr as property in css selector [duplicate]

ε祈祈猫儿з 提交于 2019-12-02 09:20:43

问题


Possible Duplicate:
CSS selector by style attribute

html:

<div data-family='arial'></div>

in css i want to use this family value. Example:

div{font-family: attr(data-family)}

Family can be any string. How make it work?


回答1:


Try this code:

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

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

JSFIDDLE




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/12895374/attr-as-property-in-css-selector

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