Get CSS pre-content using jQuery

I have the following CSS

h1:before { content: "Chapter " counter(lvl1) "\000d\000a"; counter-increment: lvl1; white-space: pre-wrap; } h1 { page-break-before: right; } 

and this HTML

 <p>...</p> <h1>A Title</h1> <p>...</p> 

With this CSS, I get something like

 Chapter 1 A Title 

When I try to get text using jQuery, I get "A Title". Is it possible to get a "Chapter 1 \ nA Title"?

thanks

+7
source share
1 answer

Use getComputedStyle() :

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

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

JS working script provided by Eric .

Despite the unsuccessful warning, this approach returns the literal string used in the content declaration, not the actual generated content.

+25
source

All Articles