正如标题所暗示的那样,我想知道如何在段落中的br之前和之后的跨度中包装文本.
我的HTML就是这样......
<div class="blog-container">
<div class="each-article">
<p>Text text text <br/> text text text</p>
</div>
<div class="each-article">
<p>Text text text <br/> text text text</p>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
而我认为可行的jQuery是
$('.blog-container .each-article p br').before().wrap('<span></span>');
Run Code Online (Sandbox Code Playgroud)
这给了我的是:
<div class="blog-container">
<div class="each-article">
<p>Text text text <br><span></span></br> text text text</p>
</div>
<div class="each-article">
<p>Text text text <br><span></span></br> text text text</p>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
试试这个:
$('.each-article p').each(function() {
$(this).contents().filter(function() {
return this.nodeType == 3;
}).wrap('<span>');
});
Run Code Online (Sandbox Code Playgroud)
参考文献:
contents(),选择所有子节点,包括文本节点3)编辑 - 此处不需要修剪.但是,在某些情况下(如果非文本节点是第一个子节点),空白空间可以视为文本节点.看到这个小提琴:http://jsfiddle.net/M3knf/
| 归档时间: |
|
| 查看次数: |
2012 次 |
| 最近记录: |