คุณสามารถเปลี่ยนองค์ประกอบแบบไดนามิกใน HTML ผ่าน JavaScript
เพิ่มองค์ประกอบใน HTML
ก่อนอื่นคุณต้องสร้างแท็กจากนั้นเพิ่มเนื้อหาที่เกี่ยวข้องลงในแท็กจากนั้นเพิ่มแท็กที่สร้างขึ้นในตำแหน่งที่สอดคล้องกัน
<! doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content-type" content = "text/html; charset = gb2312"/> <title> Document.createElement ("P"); var node = document.createtextNode ("เพิ่มย่อหน้าใหม่"); Element.AppendChild (Node); x = document.getElementById ("สาธิต"); X.AppendChild (องค์ประกอบ); } </script> </head> <body> <div id = "demo"> <p> นี่คือย่อหน้าแรก </p> </div> <อินพุต type = "button" value = "ปุ่ม" onclick = "เพิ่ม ()"/> </body> </html>ลบองค์ประกอบใน HTML
<! doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content-type" content = "text/html; charset = gb2312"/> <title> document.getElementById ("สาธิต"); var child = document.getElementById ("P1"); พ่อ. removeChild (เด็ก); } </script> </head> <body> <div id = "demo"> <p id = "p1"> นี่เป็นย่อหน้าแรก </p> <p id = "p2"> นี่คือย่อหน้าที่สอง </p> </div> <อินพุต type = "body"