简体   繁体   English

CSS列表样式类型使用属性值

[英]CSS List style type Using attribute value

I'm having some html elements like this 我有一些这样的html元素

<div class="col-md-3">
    <div class="olListPallette" data-numberStyle="counter(count, lower-alpha) '. '">type1</div>
</div>
<div class="col-md-3">
    <div class="olListPallette" data-numberStyle='counter(count, upper-alpha) ". "'>type2</div>
</div>
<div class="col-md-3">
    <div class="olListPallette" data-numberStyle='counter(count, lower-alpha) ") "'>type3</div>
</div>

What I'm trying to do is when a click a div, the attribute should be set to the <ol> . 我想做的是单击div时,属性应设置为<ol> It has a counter in css and the new list style should be applied to the list items. 它在css中有一个计数器,并且新的列表样式应应用于列表项。 Here is my list 这是我的清单

<ol>
    <li>first</li>
    <li>second</li>
    <li>thirs</li>
</ol>

and my js is 而我的js是

$('.olListPallette').click(function(){
    $('ol').attr('data-numberStyle',$(this).attr('data-numberStyle'));
});

my css: 我的CSS:

ol {
list-style: none !important;
counter-reset: count;
}
ol li:before {
content: attr(data-numberStyle);
counter-increment: count;
}

The problem is the attribute data-numberStyle doesn't get the counter value. 问题在于属性data-numberStyle没有获得计数器值。 If I give the value directly like content: counter(count, lower-alpha) ") " then it will work. 如果我直接像content: counter(count, lower-alpha) ") "一样给出值content: counter(count, lower-alpha) ") "那么它将起作用。 But I need to use the attribute value in css. 但是我需要在CSS中使用属性值。

Also, In other cases I need to use only separator. 另外,在其他情况下,我只需要使用分隔符。 So, If I use the counter in css, I need to pass the separator. 因此,如果我在CSS中使用计数器,则需要传递分隔符。

Example: 例:

content: counter(count) + attr(data-numberStyle); // this is not working
// Something like this counter(count) + ", ".
// <div data-numberStyle = ", ">

Here is my Fiddle . 这是我的小提琴 If my question is unclear, feel free to ask. 如果我的问题不清楚,请随时提出。 I hope you understand. 我希望你明白。 How to use this attribute value in CSS? 如何在CSS中使用此属性值?

Given that data attribute should be a style, An idea comes to mind, it is to modify a 'style' tag directly in the header to apply :before css, please check the updated Fiddle sample: 鉴于data属性应该是一种样式,想到一个主意,那就是直接在标头中修改一个'style'标签以应用:在CSS之前,请检查更新的Fiddle示例:

http://jsfiddle.net/cHhJ5/3/ http://jsfiddle.net/cHhJ5/3/

Javascript: 使用Javascript:

var $style;
$(document).ready(function(){
    $('.olListPallette').click(function(){
        if(!$style){
             $style = $('<style id="listStyle">ol li:before {'+$(this).attr("data-numberStyle")+' ";}</style>');
             $("head").append($style);
        }else{
             $style.html('ol li:before {'+$(this).attr("data-numberStyle")+' ";}</style>');
        }
    });
});

Hope it helps 希望能帮助到你

Edit (Explanation): 编辑(说明):

:before (and others pseudo-selectors like :after) can't be modified directly with jQuery because there aren't part of the DOM. :before(和其他类似:selector的伪选择器)不能直接用jQuery修改,因为它不包含DOM。

So, a possible workaround may be to modify directly a style tag in header to accomplish the purpose of modify the list style. 因此,一种可能的解决方法可能是直接修改标题中的样式标签,以实现修改列表样式的目的。

However, I feel that it's a bit hackish to add a style tag in header and put styles directly in data attribute, I'd rather prefer to use predefinied css classes and assing them through javascript: 但是,我觉得在标头中添加样式标签并将样式直接放置在data属性中有点琐,我宁愿使用预定义的CSS类并通过javascript对其进行赋值:

http://jsfiddle.net/cHhJ5/4/ http://jsfiddle.net/cHhJ5/4/

Html, use a class name as a data attr. html,使用类名称作为数据属性。 value: 值:

<div class="olListPallette" data-numberStyle="low">type1</div>

Css, define classes with desired styles: CSS,用所需的样式定义类:

ol.low li:before {
    content: counter(count, lower-alpha) ") ";
    counter-increment: count;
}

ol.upp li:before {
    content: counter(count, upper-alpha) ") ";
    counter-increment: count;
}

Javascript, just set related class on click: Javascript,只需在点击时设置相关类即可:

$(document).ready(function(){
    $('.olListPallette').click(function(){
        $("ol").removeClass();
        $("ol").addClass($(this).attr("data-numberStyle"));
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM