1。概要
削除されたノードはドキュメントツリーにはなくなりましたが、実際にはまだメモリにあり、いつでも別の場所に追加できます。
親ノードの子ノードをトラバースして削除操作を実行すると、子属性は読み取り専用属性であり、子ノードが変更されるとリアルタイムで更新されることに注意してください。
//ノードを削除する取得:var self = document.getElementbyId( 'to-be-removed'); //親ノードを取得:var parent = self.parentelement; // delete:var removed = parent.removechild(self);削除=== self; // 真実
2。例
<!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 document.getElementById( 'test-list'); var length = p.children.length; var i = 0; for(; i <length;){var li = p.children [i]; var text = li.innertext; if(text!== 'javascript' && text!== 'html' && && text!== 'css'){p.removechild(li); alert(p.children.tostring()); length - ;} else {i ++;}} // test:;(vararr、i、t = document.getelementbyid( 'test-list'); if(t && t.children = = 3) []; for(i = 0; i <t.children.length; i ++){arr.push(t.children [i] .innertext);} if(arr.tostring()== ['javascript'、 'html'、 'css']。 arr.toString());}} else {alert( 'test failed!');}})(); </script> </body> </html>上記は、編集者が紹介したJavaScriptの削除メソッド開発のサンプルコードです。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!