2013-02-16 112 views
7

CSS的,我有以下CSS之前把內容用jQuery

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

這個HTML

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

在給定的CSS我得到類似

Chapter 1 
A Title 

當我嘗試使用jQuery獲取文本我得到「標題」。是否有可能獲得「第1章\ nA標題」?

由於

回答

25

使用getComputedStyle()

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

before()是用於遍歷DOM一個jQuery方法,它獲取/設置前一元素,但它確實不訪問僞元件。

Working JS Fiddle,由Eric提供。

雖然有不幸的警告,這種方法返回的content聲明中使用的文字字符串,而不是實際生成的內容。

+0

工作小提琴,對於任何感興趣的人:http://jsfiddle.net/YdMcv/2/ – Eric 2013-02-16 17:56:23

+0

其實,這不是很正確,因爲它從CSS文件中獲得文字「:before」的內容,而不是實際生成的內容。 OP希望'Chapter 1',而不是'Chapter' counter(lvl1)'......很酷的答案,但我認爲它不會有用。 – 2013-02-16 17:57:42

+0

@Eric:謝謝你,編輯。 – 2013-02-16 17:57:48

相關問題