Home  >  Article  >  Web Front-end  >  The use of dynamically creating and deleting elements in DOM-based programming_Basic knowledge

The use of dynamically creating and deleting elements in DOM-based programming_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:36:36946browse
Copy code The code is as follows:



< ;script type="text/javascript">
function test(){
//createElement() Create an element with a specified tag name [for example: dynamically create a hyperlink]
var createa=document. createElement("a");
createa.id="a1";
createa.innerText="Connect to Baidu";
createa.href="http://www.baidu.com";
//createa.color="green" ////Add color (don't forget the style attribute, otherwise it will have no effect)
createa.style.color="green"
//Add default location-- body and add child nodes
//document.body.appendChild(createa);
//Place the specified position
>
function test2(){
//Delete node at specified position removeChild()
document.getElementById("div1").removeChild(document.getElementById("a1")); //Duplicate id name js only takes the first
}


















< input type="button" value="Create an a tag" onclick="test()"/>








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