JavaScript를 통해 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> 테스트 </title> <script type/javascrict "> var elegel = {) 문서 .CreateElement ( "P"); var node = document.creatextNode ( "새 단락 추가"); 요소 .appendChild (노드); x = document.getElementById ( "데모"); X. AppendChild (요소); } </script> </head> <body> <div id = "demo"> <p> 이것은 첫 번째 단락 </p> </div> <입력 유형 = "value ="button "onclick ="add () ""/> </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> 테스트 </title> <script type/javaScript "> 함수 deletee () document.getElementById ( "데모"); var child = document.getElementById ( "P1"); 아버지 .removechild (자녀); } </script> </head> <body> <div id = "demo"> <p id = "p1"> 이것은 첫 번째 단락 </p> <p id = "p2"> 이것은 두 번째 단락 </p> </div> <입력 유형 = "value ="delete "onclick ="deletee () "/body> </html>입니다.