0

我正在使用此代码段以删除断点 ( <br/>) 并将我的文本包装在段落中:

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

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

问题是我的文本包含一个未包含在段落中的超链接 () - 它只是被遗漏了......

这是我的文本片段:

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>

这怎么可能...?

4

1 回答 1

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>


如果您对此有任何疑问,请告诉我。祝你好运!:)

于 2013-04-11T14:03:03.000 回答