JavaScript HTML DOM ??(??)
HTML ?? ??
var newEle =document.createElement(p);
??? ??? ?? ??? ?????.
A.appendChild(B): B? ? ??? ?? A ?? ?? B? ??? ?? ?? ?? ?? ?????.
??? B? ??? ?? ?? ?? ? ?? ??? B ??? A? ?? ??? ???? ????.
appendChild() ? ??? innerHTML ??? ??? ??? ????. ???? ??? ????.
1 innerHTML?appendChild?? ??? ?????(??? ?? ??? ???? ??? ? ????).
2 innerHTML? ??? ??? ??appendChild?? ? ?????. ??
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <div id="div1"> <p id="p1">這是一個(gè)段落。</p> <p id="p2">這是另一個(gè)段落。</p> </div> <script> var para=document.createElement("p"); var node=document.createTextNode("這是一個(gè)新段落。"); para.appendChild(node); var element=document.getElementById("div1"); element.appendChild(para); </script> </body> </html>
?? ??:
? ??? ???
??? ?????:
var para=document.createElement("p");
??? ???? ????? ?? ???? ???? ???. ??. ? ??? ??? ??? ?????:
var node=document.createTextNode("This is a new Parallel.");
?? ?? ? ??? ???
??? ???? ???:
para.appendChild (node);
????? ?? ??? ? ??? ???? ???.
? ??? ?? ??? ????.
var element=document.getElementById("div1");
?? ??? ?? ?? ?? ? ??? ?????.
element.appendChild(para );
Remove HTML elements
removechild ??? ?? ??? ??? ?? ??? ??? ? ????.
? ??? ?? ??? ????? ???? ??? ??? ????, ??? ??? null? ?????.
?? ??:
fatherObj.removeChild(childrenObj)
???? ??:
1.fatherObj: ??? ?? ??? ?? ?????.
2.childrenObj: ??? ?? ?? ?????.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <div id="div1"> <p id="p1">這是一個(gè)段落。</p> <p id="p2">這是另一個(gè)段落。</p> </div> <script> var parent=document.getElementById("div1"); var child=document.getElementById("p1"); parent.removeChild(child); </script> </body> </html>
?? ??
? HTML ???? ? ?? ?? ??(? ?? <p> ??)? ?? <div> ??? ???? ????.
<div id="div1"> <p id="p1">這是一個(gè)段落。</p> <p id="p2">這是另一個(gè)段落。</p> </div>
id="div1"? ?? ??:
var parent=document.getElementById("div1");
id="p1"?
?? ??:
var child=document.getElementById("p1");
?? ???? ?? ?? ??:
parent.removeChild(child);