Get Before-Content of CSS with jQuery

后端 未结 1 1013
轻奢々
轻奢々 2020-12-08 12:11

I have the following CSS

h1:before {
    content: \"Chapter \" counter(lvl1) \"\\000d\\000a\";
    counter-increment:         


        
相关标签:
1条回答
  • 2020-12-08 12:31

    Use getComputedStyle():

    $('h1').each(function(){
        console.log(window.getComputedStyle(this,':before').content);
    });
    

    before() is a jQuery method used to traverse the DOM, it gets/sets the preceding element, it does not access the pseudo-element.

    Working JS Fiddle, supplied by Eric.

    Though there is the unfortunate caveat that this approach returns the literal string used in the content declaration, rather than the actual generated-content.

    0 讨论(0)
提交回复
热议问题