1. Descripción general
Aunque el nodo eliminado ya no está en el árbol de documentos, en realidad todavía está en la memoria y se puede agregar a otra ubicación en cualquier momento.
Cuando atraviese el nodo secundario de un nodo principal y realice una operación de eliminación, tenga en cuenta que el atributo infantil es un atributo de solo lectura y se actualizará en tiempo real cuando cambie el nodo secundario.
// Haga que el nodo se elimine: var self = document.getElementById ('to-be-removed'); // Obtenga el nodo principal: var parent = self.ParentElement; // delete: var removed = parent.removechild (self); eliminado === self; // verdadero2. Ejemplo
<! Doctype html> <html> <head> </script> </head> <body> <ul id = "test-list"> <li> javascript </li> <li> swift </li> <li> html </li> <li> ansi c </li> <li> css </li> <li> Directx </li> </sits> <scry> <//script <script <script <script ul> document.getElementById ('test-list'); var longitud = p.children.length; var i = 0; for (; i <longitud;) {var li = p.children [i]; var text = li.innertext; if (text! == 'javaScript' && text! == 'html' && text! == 'css') {p.removechild (li); alert (p.children.ToString ()); longitud-;} else {i ++;}} // test:; (function () {vararr, i, t = document.getElementByid ('testlist'); if (t && t.children && t.children.length longitud []; for (i = 0; i <t.children.length; i ++) {arr.push (t.children [i] .innertext);} if (arr.ToString () === ['javaScript', 'html', 'css']. toString ()) {'test arr.ToString ());}} else {alert ('test fallido!');}}) (); </script> </body> </html>Lo anterior es el código de muestra para el desarrollo del método RemovalChild en JavaScript presentados por el editor. Espero que te sea útil. Si tiene alguna pregunta, déjame un mensaje y el editor le responderá a tiempo. ¡Muchas gracias por su apoyo al sitio web de Wulin.com!