1.
DOM知识的翻译与整理2.
下面是我的一个应用:
<script>
function show_another(id) {
otable = document.getElementById(id);
otable_c = otable.cloneNode(true);//复制一个节点
div_id = id+'s';
odiv = document.getElementById(div_id);
//下变是为了给添加的节点(这里是个table) 前加个计数的标题(其实这里它是个div节点,在页面中已存在)
otables = odiv.getElementsByTagName("table");//要填元素标签名
len = otables.length;
odiv_title = document.getElementById("div_title");
odiv_title_c = odiv_title.cloneNode(true);//复制另一个节点
odiv_title_c_child = odiv_title_c.childNodes[0];
odiv_title_c_child.nodeValue = "增加教育经历"+len;//改变文本节点的值
odiv_title_c.setAttribute("className","addtitle");//添加css
otable_c = emptyNodeVaule(otable_c);//如果要复制的节点已被填入了值,复制时这些值也会被复制过来,这里要清空复制的节点中的input和textarea的值。
odiv.appendChild(odiv_title_c);//追加计数的标题(一个div节点)
odiv.appendChild(otable_c);//追加已被清空的table节点
}
function emptyNodeVaule(node) {
var children = node.childNodes;
for(var i = 0; i < children.length; i++) {
if(children[i].tagName == 'INPUT') {//可得input的值选取,注意:与textarea的不同
//alert(children[i].tagName);
children[i].setAttribute("value","");
} else if (children[i].tagName == 'TEXTAREA') {
children[i].value = '';
//children[i].childNodes[0].nodeValue = "";//可得textarea的值选取
}
emptyNodeVaule(children[i]);//递归操作
}
return node;
}
</script>