In diesem Artikel wird beschrieben, wie JS das Hinzufügen, Ersetzen und Löschen von Knotenelementen implementiert. Teilen Sie es für Ihre Referenz wie folgt weiter:
Ich war immer verwirrt über Knotenoperationen, insbesondere nachdem ich ihn in einen bestimmten Knoten eingefügt habe. Es gibt keine solche Methode. Ich habe ein Problem mit der Methode, die ich zuvor geschrieben habe. Soll ich den neuen Knoten in den alten Knoten einfügen? Oder sollte ich die Insert -vor -Methode verwenden, um sie zu implementieren.
Hier ist die Methode:
<! DocType html public "-// w3c // dtd html 4.01 transitional // en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <! Infütterung </title> </head> <body onload = "InsertMessAGEAnl ()"> <p id = "p1"> Hallo Welt </p> </body> </html> <script> Funktion removemessage () {var op = document.getElementByIdx_x ("p1"); //document.body.removechild(OP); //op.parentnode gibt den übergeordneten Klassenknoten op.parentnode.removechild (op) zurück; } function ersatzemessage () {var newoP = document.createelement_x ("p"); Newop.AppendChild (document.CreateTextNode ("Hallo Java")); //alert(newop.innerhtml); var oldop = document.getElementByIdx_x ("p1"); //document.body.removechild(OP); //op.parentnode gibt den übergeordneten Klassenknoten oldop.parentnode.replacechild (Newop, Oldop) zurück; //document.body.Replacechild(newop, altep)} Funktion InsertMessageBefore () {var newop = document.createelement_x ("p"); Newop.AppendChild (document.CreateTextNode ("Hallo Java")); var oldop = document.getElementByIdx_x ("p1"); oldop.parentnode.insertbefore (newop, Oldop); } function InsertMessAGEER () {var newoP = document.createelement_x ("p"); Newop.AppendChild (document.CreateTextNode ("Hallo Java")); var oldop = document.getElementByIdx_x ("p1"); InsertAfter (Newop, Oldop); } function InsertBefore (newnode, OldNode) {OldNode.ParentNode.insertBefore (newnode, OldNode); } function InsertAfter (NewNode, OldNode) {// Beurteilen Sie, ob es Tags derselben Kategorie nach dem Oldnode var nextnode = oldNode.NextSsibling gibt; if (NextNode) {// Wenn nicht, ist es null, es ist falsch, wenn es vorhanden ist, ist es wahr alteNode.parentnode.insertbefore (newnode, NextNode); } else {OldNode.ParentNode.AppendChild (newnode); }} </script>For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript Operation DOM Skills", "Summary of JavaScript Replacement Operation Techniques", "Summary of JavaScript Value Transfer Operation Techniques", "Summary of JavaScript Encoding Operation Techniques", "Summary of JSON Operation Techniques in JavaScript", "JavaS Summary of script switching effects and techniques", "Summary of JavaScript search algorithm skills", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm techniques", "Summary of JavaScript traversal algorithms and techniques", and "Summary of JavaScript mathematical operations usage"
Ich hoffe, dieser Artikel wird für JavaScript -Programme aller hilfreich sein.