2013-02-16 49 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

相关问题