2013-04-11 26 views
0

我玩一起在這個片段中,以去除斷點(<br/>),並纏上了我的文字段落:包括超級鏈接<a> jQuery中。內容

$('.articleText').contents().filter(function() { 
    return this.nodeType == 3; 
}).wrap('<p></p>').end().filter('br').remove(); 

來源:http://api.jquery.com/contents/

的問題是我的文本包含一個超鏈接(),這是不包括在本段 - 它只是被排除在外...

這是我的textsnippet:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. <br/><br/>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a <a href="#">galley</a> of type and scrambled it to make a type specimen book.

這是它是如何轉化:

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p> 
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a</p> 
<a href="#">galley</a> 
<p>of type and scrambled it to make a type specimen book.</p> 

,這是應該的:

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p> 
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a <a href="#">galley</a> of type and scrambled it to make a type specimen book.</p> 

這怎麼可能......?

回答

0

方法.contents()只返回子女的內容,不是所有的後代。由於您的<a>標籤嵌套在子標籤中,因此.contents()方法不會採用該標籤。

試試這個:

$('.articleText').contents().add($(this).find("*").contents())... 

注:我還沒有測試上面,所以它可能需要一些小的調整。

更新:如果我誤解你,是不應該的<a>標籤被嵌入,那麼你必須在你的標記錯誤:

嘗試改變:

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.<p>

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>


讓我知道你是否對此有任何疑問。祝你好運! :)

+0

還沒有運氣 - 我的標記中有一個錯誤,現在正在糾正...... - 我的問題基本上是:「如何將純文本和鏈接都包含在同一段中??」 - 到目前爲止,我的鏈接站在段落之外... – 2013-04-24 13:01:02