从DOM中删除标记

时间:2014-05-28 13:46:06

标签: javascript jquery html dom

我需要使用jQuery或JavaScript从DOM中删除单个标记。我只需要删除封闭标记,标记内的内容。

remove()detach()empty()方法无法应用,因为它们也会删除整个内容。

例如:

<table border="1" style="width:300px">
<tr>
  <td>Jill</td>
  <td>Smith</td> 
  <td>50</td>
</tr>
<tr>
  <td>Eve</td>
  <td>Jackson</td> 
  <td>94</td>
</tr>
</table>

只需删除<table></table>

目前DOM看起来像这样

<table style="width:600px">
<tbody><p> Number  -  Name  -   <br></p>
    <p> 111  -  ABC  -  <br></p>
    <p> 222  -  KLM  -  <br></p>
    <p> 333  -  NOP  - <br></p>
    <p> 444  -  HIJ  -  <br></p>
</tbody></table>

提前谢谢。

4 个答案:

答案 0 :(得分:5)

尝试.unwrap(),但结果将是无效的HTML,

$('tr').unwrap();

答案 1 :(得分:1)

var text=$("table").text();
$("table").parent().html(text);// now at the place of text come 

答案 2 :(得分:1)

$("table").replaceWith($("table").html());

答案 3 :(得分:1)

使用Jquery:

$('table').each(function(){
    $(this).replaceWith($(this).html());
});