2013-04-11 75 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