Javascript--DOM

it2024-11-15  8

获取一个元素:

document.getElementById(nodeid);

创建一个元素:

document.createElement(tag);

创建文本节点:

document.createTextNode(text);

设置元素属性:

document.getElementById(nodeid).setAttribute(attribute,value);

插入一个元素:

parentOfbeforeNode.inertBefore(nodeToInsert,beforeNode);

在元素结尾插入一个元素:

parentOfNode.appendChild(nodeToInsert);

删除一个元素:

parentOfNode.removeChild(node);

例子:

<div id="dv"> this is a div </div> <input type="button" id="add" value='Add' οnclick="addNode()" /> <input type="button" id="delete" value='delete' οnclick="deleteNode()" /> <script type="text/javascript"> function addNode(){ var dv = document.getElementById("dv"); var btn = document.createElement("input"); btn.type='button'; btn.id='btn'; btn.value='add'; dv.parentNode.insertBefore(btn,dv); } function deleteNode(){ var btn = document.getElementById("btn"); if(btn){ btn.parentNode.removeChild(btn); } } </script>

转载于:https://www.cnblogs.com/oneword/archive/2009/12/07/1618738.html

最新回复(0)