Artikel ini menjelaskan bagaimana JS mengimplementasikan penambahan, mengganti, dan menghapus elemen simpul. Bagikan untuk referensi Anda, sebagai berikut:
Saya selalu bingung tentang operasi simpul, terutama setelah memasukkannya ke dalam simpul tertentu. Tidak ada metode seperti itu. Saya memiliki masalah dengan metode yang saya tulis sebelumnya. Haruskah saya memasukkan simpul baru ke dalam simpul lama? Atau haruskah saya menggunakan metode insertBefore untuk mengimplementasikannya.
Inilah metodenya:
<! Doctype html public "-// w3c // dtd html 4.01 transitional // en" "http://www.w3.org/tr/html4/loose.dtd"> <headsoft> <!-Dibuat oleh Topstyle Pro Trial Version-WWW.BW.BW. Sisipkan </itement> </head> <body onload = "InsertMessageAfter ()"> <p id = "p1"> halo world </p> </body> </html> <script> function removemessage () {var op = document.getElementByIdx_x ("p1"); //document.body.removechild(op); //op.parentnode Mengembalikan node kelas induk op.parentnode.removechild (OP); } function replaceMessage () {var newop = document.createelement_x ("p"); newop.appendChild (document.createTextNode ("Hello Java")); //alert(newop.innerhtml); var oldop = document.getElementByIdx_x ("p1"); //document.body.removechild(op); //op.parentnode Mengembalikan node kelas induk oldop.parentnode.replacechild (newop, oldop); //document.body.replacechild(newop,oldop)} fungsi insertMessageBefore () {var newop = document.createelement_x ("p"); newop.appendChild (document.createTextNode ("Hello Java")); var oldop = document.getElementByIdx_x ("p1"); oldop.parentnode.insertbefore (newop, oldop); } function insertMessageAfter () {var newop = document.createelement_x ("p"); newop.appendChild (document.createTextNode ("Hello Java")); var oldop = document.getElementByIdx_x ("p1"); insertAfter (newop, oldop); } fungsi insertBefore (newNode, oldnode) {oldnode.parentnode.insertBefore (newnode, oldnode); } fungsi insertAfter (newNode, oldnode) {// menilai apakah ada tag dari kategori yang sama setelah oldnode var nextNode = oldnode.nextsibling; if (nextNode) {// jika tidak, itu nol, itu salah, jika ada, itu benar oldnode.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", Ringkasan Keterampilan Algoritma Pencarian JavaScript "," Ringkasan Efek dan Teknik Animasi Javascript "," Ringkasan Kesalahan JavaScript dan Teknik Debugging "," Ringkasan Struktur Data Javascript dan Teknik Algoritma "Ringkasan Javascrips of Javascrips Algripsal dan Ringkasan Javascrips" Javascrips Algripsal dan Ringkasan "Javascrips" Javascrips "Javascrips" Javascrips "Javascrips" Javascrips "Javascrips" Ringkasan "Ringkasan" Javascrip "Ringkasan" Javascrip "
Saya harap artikel ini akan membantu pemrograman JavaScript semua orang.