标签:
//具体关于DOM的内容可参考我的另外一篇文章“文本对象模型(Document Object Model)”.
<ul id="ul">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<script>
var parentElement = document.getElementById("ul");
var NewNode = document.createElement("li");
NewNode.innerText="AddNode";
parentElement.appendChild(NewNode);
</script>
<ul id="ul">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
- <ol id="ol"></ol>
<script>
var parentElement = document.getElementById("ul");
var parentElement2=document.getElementById("ol");
if (parentElement.hasChildNodes()){
alert("<ul>有子节点");
}
if(parentElement2.hasChildNodes()){
alert("<ol>有子节点");
}
</script>
<ul id="ul">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<script>
var parentElement = document.getElementById("ul");
var secondNode = parentElement.getElementsByTagName("li")[1];
var NewNode = document.createElement("li");
NewNode.innerText = "insertNode";
parentElement.insertBefore(NewNode, secondNode); //将NewNode插入在secondNode前面
</script>
<ul id="ul">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
>
var parentElement = document.getElementById("ul");
var secondNode = parentElement.getElementsByTagName("li")[1];
var NewNode = document.createElement("li");
NewNode.innerText = "replaceNode";
parentElement.replaceChild(NewNode, secondNode); //将secondNode替换成NewNode
</script>
<ul id="ul">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<script>
var parentElement = document.getElementById("ul");
var secondNode=parentElement.getElementsByTagName("li")[1];
parentElement.removeChild(secondNode);
</script>
标签:
原文地址:http://www.cnblogs.com/Jener/p/5954038.html