死亡空间2节点,javascript--节点(2)

<html>
<head>
<script type="text/javascript">
window.onload=function (){
var header=document.getElementById("header");
alert(header);
// alert(header.getAttribute("class")) //在ie中不可用,要写className。在火狐中可用
/*在IE中
class className
for htmlFor
等等
*/
////一个通用的方法////
var specialNames={
"class":"className",
"for":"htmlFor"
};
function getAttr(node,attrName){
var attr=node.getAttribute(attrName);
if(attr==null){
if(attrName in specialNames){
/////attrName in specialNames attrName是否在specialNames 返回true和false
attrName=specialNames[attrName];
attr=node.getAttribute(attrName)
}
}
return attr;
}
alert(getAttr(header,"class")) ////在IE中可以了
/////////节点的克隆/////
var clone=document.getElementById("clone");
var nop=clone.cloneNode(true);
document.body.appendChild(nop) ;
////appendChild///如果想添加多个需要一条一条的写,很烦,解决方法
var div1=document.createElement("div");
var textNode=document.createTextNode("aaaaa")
var textNode2=document.createTextNode("bbbbb")
var textNode3=document.createTextNode("ccccc")
div1.appendChild(textNode);
var div2=document.createElement("div");
div2.appendChild(textNode2);
var div3=document.createElement("div");
div3.appendChild(textNode3);
function append(node){
for(var i=1;i<arguments.length;i++){
node.appendChild(arguments);
}
return node;
}
append(document.body,div1,div2,div3)
//////removeChild//同样写函数完成批量删除///
function delNode(){
for(var i=0;i<arguments.length;i++){
arguments.parentNode.removeChild(arguments)
}
}
delNode(div1,div2)
//////replaceChild写成函数、、、
function rep(newNode,replaced){
replaced.parentNode.replaceChild(newNode,replaced)
}
rep(div1,div3) //newNode 是新的 替换掉resplaced 并占用resplaced的位置
}
</script>
<title></title>
</head>
<body>
<p id="header" class="cs">节点二</p>
<h1 id="clone">节点的克隆 -- node.cloneNode(true or false)</h1>
true:连同子节点一起克隆</br>
false:不克隆子节点</br>
</body>
</html>
Tags: 

延伸阅读

最新评论

发表评论