如何删除元素但保留其子元素?

时间:2016-08-22 02:51:21

标签: javascript jquery html

我有这样的HTML:

<a href="#">
    <span>children</span>
</a>
<button>click</button>

我想删除<a>但请保留<span>。我怎么能这样做?

$('button').on('click', function(){
   /* to remove the link, but not the span */
});

2 个答案:

答案 0 :(得分:1)

我会做类似的事情:

$a = $("a")
$a.replaceWith($a.children());

答案 1 :(得分:0)

&#13;
&#13;
$('button').on('click', function(){
   /* to remove the link, but not the span */
   
  var inner_html = $("#my_anchor").html();
  $("#parent").html(inner_html)
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parent">
  
<a href="#" id="my_anchor">
    <span>children</span>
</a>
  
</div>

<button>click</button>

   
&#13;
&#13;
&#13;