Home >Web Front-end >JS Tutorial >JavaScript DOM node addition example_javascript tips

JavaScript DOM node addition example_javascript tips

WBOY
WBOYOriginal
2016-05-16 16:41:531362browse
<!DOCTYPE html> 
<html> 
<body> 

<div id="div1"> 
<p id="p1">这是一个段落。</p> 
<p id="p2">这是另一个段落。</p> 
</div> 

<script> 
var para=document.createElement("h1"); 
var node=document.createTextNode("这是新段落。"); 
para.appendChild(node); 
//将node里面的str放到h1段落中 
//使之成为<h1>这是新段落</h1> 

var element=document.getElementById("div1"); 
element.appendChild(para); 

//将para放到div中 

</script> 

</body> 
</html>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn