Eliminar una etiqueta del DOM [duplicado]

I need to remove a single tag from the DOM using jQuery or JavaScript. I only need to remove the enclosing tag, no the content inside the tag.

remove(), detach(), empty() methods cannot be applied as they remove the whole content as well.

por ejemplo:

<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>

need to remove only the <table></table>.

Currently DOM looks like this

<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>

Gracias de antemano.

preguntado el 28 de mayo de 14 a las 14:05

If you removed the table element and left the table row elements behind then the document would be invalid. Why would you want that? -

I agree with @Quentin. You shouldn't do it, but for this specific example, you could do something like var table = document.querySelector("table"); table.outerHTML = table.innerHTML; -

No all these table data needs to be represented in paragraphs. Now I get the paragraphs inside a cage. I want to remove that. for that I need to remove the table tag as well. <th> <td> all are removed in the DOM. -

What does the HTML look like now and what do you want it to look like? -

@user3547920 if you're counting on the <tr> y <td> being removed as well, how about using the .text() method to extract the text and then replace in a paragraph? Is that the intent? It would be useful if you included what your desired resulting DOM would be. -

4 Respuestas

Trate de .unwrap() it, but the result would be an invalid html,

$('tr').unwrap();

contestado el 28 de mayo de 14 a las 14:05

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

contestado el 28 de mayo de 14 a las 14:05

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

contestado el 28 de mayo de 14 a las 14:05

Con Jquery:

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

contestado el 28 de mayo de 14 a las 14:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.