1

HTML:

<div class="featured">
    <h4><a href="#">Title 1</a></h4>
    <a href="#"><img src="image.png" class="image"/></a>
    <p><a href="#"></a><br />
    Description goes here</p>
</div>

<div class="featured">
    <h4><a href="#">Title 2</a></h4>
    <a href="#"><img src="image.png" class="image"/></a>
    <p><a href="#"></a></p>
    <p>Description goes here</p>
</div>

.. 如何<p>从 .featured 中删除所有标签?

谢谢

4

5 回答 5

2

这有效,但只是因为您的段落元素位于 div 的末尾:

$(".featured p").each(function(){
    var content = $(this).html();
    $(this).parent().append(content);
    $(this).remove();
});
于 2010-01-28T12:37:41.527 回答
2

查看Ben Alman 的展开“插件”

$.fn.unwrap = function() {
  this.parent(':not(body)')
    .each(function(){
      $(this).replaceWith( this.childNodes );
    });

  return this;
};

你的用法是:

$(".featured p > *").unwrap();
于 2010-01-28T13:04:14.973 回答
1
$(".featured p").remove();

这通过将每个更改<p><span>. 它就地执行此操作,因此<p>s 不必在末尾(没有附加 - 它不会重新排序元素):

$(".featured p").replaceWith(function(){
     return $('<span />').html( $(this).html() );
});

它使用 html,因此元素将丢失数据和事件绑定。

于 2010-01-28T12:16:44.257 回答
1

像这样的东西?

$(".featured p").each(
  function(){
    $(this).after($(this).html()).remove();
});

编辑2:经过测试,有效。又好又简单。

于 2010-01-28T12:19:16.107 回答
1

使用 jQuery 1.4 你可以这样做:

$(".featured p *").unwrap();
$(".featured p").each(function(){
  $(this).replaceWith("<span>"+ $(this).text() + "</span>")
});

在这里测试

文本节点不会被展开,因此您必须单独执行它们。我不确定为什么 replaceWith 要求它们在标签内。

于 2010-01-28T12:54:21.477 回答