jquery - 将文本包裹在元素内

标签 jquery

我需要将文本包裹在带有跨度的 div 内。

<div class="item">
   <span class="count"></span>
   Text that needs to be wrapped.
</div>

<div class="item">
   <span class="count"></span>
   Text that needs to be wrapped.
</div>

<div class="item">
   <span class="count"></span>
   Text that needs to be wrapped.
</div>

尝试过,但并没有真正起作用......

$('.item').text().wrap('<span class="new" />'); 

最佳答案

您可以使用contents()来做到这一点和 .eq()

$('.item').each(function(i, v) {
    $(v).contents().eq(2).wrap('<span class="new"/>')
});​

http://jsfiddle.net/qUUbW/

关于jquery - 将文本包裹在元素内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13652140/

相关文章:

javascript - “$', ' 文档”、 'window' 未定义

jQuery每个方法没有返回值

javascript - div 在另一个动态 div 的底部

javascript - 如何在javascript中获取内部div的值

javascript - 将图标保留在帖子底部,并在框中显示动态文本

javascript - 将 PHP 逻辑实现到 jQuery

javascript - 使用 jQuery 遍历 JSON 数组

jquery - 奇怪的晃动,当显示 child 元素时

javascript - 数组切片返回 [object Object] 而不是值

javascript - 使用 JS 从数组中弹出元素