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.
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.
相关文章推荐
- ★ Learn how you can use Adobe Creative Suite to create skins for Flex and AIR applications.
- How to Create and Use the DLL
- How to create a DLL library in C and then use it with C#
- How to create aligned partitions in Linux for use with NetApp LUNs, VMDKs, VHDs and other virtual di
- Create intelligent Web spiders -How to use Java network objects and HTML objects
- Use the Profile Manager to create and remove Firefox profiles
- How to create a DLL library in C and then use it with C#
- how-to-create-and-use-your-own-icon-fonts
- How to Create, Use and Maintain DataStage 8 Parameter Sets
- How to create fully custom Role, User, Event, Resource classes for use with the Security and Scheduler modules
- How to create and use SDCard image for Android emulator
- [Mootools] Simple tutorial of how to use domReady, DOM operation and Ajax in Mootools
- How to use outline levels to create a table of contents (TOC) in Word 2003 and in Word 2002
- [转]POI : How to Create and Use User Defined Functions
- How to Install and Use JRebel with WebLogic and Eclipse
- Why Ceph and how to use Ceph?
- How to create a project from existing source in Eclipse and then find it?
- How To Create a Child Domain in Active Directory and Delegate the DNS Namespace to the Child Domain
- How to create and start VirtualBox VM without GUI
- Responsive Web Design: What It Is and How To Use It