Eliminar una etiqueta del DOM [duplicado]
Frecuentes
Visto 135 equipos
-1
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.
4 Respuestas
5
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
1
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
1
$("table").replaceWith($("table").html());
contestado el 28 de mayo de 14 a las 14:05
1
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 javascript jquery html dom or haz tu propia pregunta.
If you removed the table element and left the table row elements behind then the document would be invalid. Why would you want that? - Quentin
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;
- nduggerNo 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. - user3547920
What does the HTML look like now and what do you want it to look like? - putvande
@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. - artlung