您的位置:首页 > 其它

how to use Dom to create and remove element

2013-10-19 13:47 507 查看
<!--step:1. createElement:tag; 2. createTextNode; 3. add textNode to Element tag; 4. get to be added tag value:

5. add the new element to the exact position;-->

<html>

<head>

<title>TestJS</title>

</head>

<body>

<div id="div1">

<p id="p1">This is the first paragraph</p>

<p id="p2">This is another paragraph</p>

</div>

<script type="text/javascript">

var newEle=document.createElement("h1");

var para=documentcreateTextNode("This is your headline");

newEle.appendChild(para);

<!--find the location-->

var oldEle=document.getElementById("div1");

oldEle.appendChild(newEle);

</script>

</body>

</html>

<!-- how to remove an element-->

<html>

<head>

<title>Remove an Element</title>

</head>

<body>

<div id="div1">

<p id="p1">This is a paragraph</p>

<p id="p2">This is another paragraph</p>

</div>

<script type="text/javascript">

var parent=document.getElementById("div1");

var child=document.getElementById("p2");

parent.removeElement(child);

</script>

</body>

</html>

always remember the steps.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: