psb

具体节点的操作技术查看php 的 DOM文档 其在JS dom中没有提出
还要注意这些操作是在内存中本地化实现,因此在查看源代码中看不到新添加的相关内容code

注意用四个节点属性:

createElement() 方法创建新的元素节点:

appendChild() 方法向已存在的节点添加子节点。(发射子弹)

removeChild() 方法删除指定的节点。 (子弹消失)

parentNode 属性可返回某节点的父节点。

<script type=”text/javascript”>

function test1(){

//window.alert(‘ok’);

//1创建 a 元素 createElment(标签名),

var myhref=document.createElement(“a”); //<a >???</a>

myhref.innerText=”连接到sina”;

myhref.href=”http://www.baidu.com

myhref.id=”myhref”;

//document.body.appendChild(myhref);

div1.appendChild(myhref);

}

 

function test2(){

//document.getElementById(‘div1’).removeChild(document.getElementById(‘myhref’));

var node=document.getElementById(‘myhref’);

node.parentNode.removeChild(node);

}

 

</script>

<body>

<input type=”button” value=”创建一个a标签” onclick=”test1()”/><br/>

<input type=”button” value=”删除a标签” onclick=”test2()”/><br/>

<div id=”div1″ style=”width:200px;height:200px;background-color:green”>div1</div>

</body>

发表回复