2011-12-29 59 views
0

我在我的網站中使用ckeditor,它支持「分頁符」。樣本內容包含頁面破乳劑,如:如何通過「分頁符」分割一些html內容?

<h1> 
    Little Red Riding Hood</h1> 
<p> 
    &quot;<b>Little Red Riding Hood</b>&quot; is a famous <a href="http://en.wikipedia.org/wiki/Fairy_tale" title="Fairy tale">fairy tale</a> about a young girl&#39;s encounter with a wolf. The story has been changed considerably in its history and subject to&nbsp;numerous modern adaptations and readings.</p> 

<div style="page-break-after: always;"> 
    <span style="display: none;">&nbsp;</span></div> 

<p> 
    The version most widely known today is based on the <a href="http://en.wikipedia.org/wiki/Brothers_Grimm" title="Brothers Grimm">Brothers Grimm</a> variant. It is about a girl called Little Red Riding Hood, after the red <a href="http://en.wikipedia.org/wiki/Hood_%28headgear%29" title="Hood (headgear)">hooded</a> <a href="http://en.wikipedia.org/wiki/Cape" title="Cape">cape</a> or <a href="http://en.wikipedia.org/wiki/Cloak" title="Cloak">cloak</a> she wears. The girl walks through the woods to deliver food to her sick grandmother.</p> 

這種特殊的div是頁斷路器

<div style="page-break-after: always;"> 
    <span style="display: none;">&nbsp;</span></div> 

我想使用JavaScript的網頁斷路器的內容爲多頁拆分,告訴用戶將有多少頁面的內容,並且可以讓用戶預覽每一頁。

但如何檢查和分割它?是否有純JavaScript或「Extjs」的解決方案?

+1

嗨。你不是試圖用split方法使用regexp嗎? – 2011-12-29 14:32:04

+0

我剛剛用正則表達式來處理它 – Freewind 2011-12-29 14:41:57

回答

1

對於此:container.innerHTML.split('<div style="page-break-after: always;">');,您可以使用String.prototype.split();函數,假定您的HTML代碼的容器爲container

您可以使用正則表達式此:

var regexp = /<div\s+style=('")page-break-after:\s*always;?.*?$1[^>]*>.*?<\/div>/gm; 

這將找到所有你斷路器的DIV。然後你可以使用它來分割字符串。

+0

是的,你的比我的好 – Freewind 2012-02-15 15:08:58

-1

HTML:

<form name="myform"> 
    <input type="checkbox" name="mybox" onClick="breakeveryheader()"> 
</form> 

的JavaScript:

function breakeveryheader(){ 
    var thestyle=(document.forms.myform.mybox.checked)? "always" : "auto" 
    for (i=0; i<document.getElementsByTagName("H2").length; i++) 
     document.getElementsByTagName("H2")[i].style.pageBreakBefore=thestyle 
} 
0

使用正則表達式:

content.split(/<div style="page-break-after: always;">[\s\S]*?<\/div>/) 

是否有[\s\S]部分更好的辦法?我認爲這有點奇怪。

+0

由於'\ s'和'\ S'是互斥的並且將通用字符集合在一起,所以您可以編寫'。*?'。看看我的正則表達式[下面](http://stackoverflow.com/a/8669243/1048157)。 – 2011-12-29 14:45:59

相關問題