一、DOM对象控制HTML
1.方法:
getElementsByName() 获取name
getElementsByTagName() 获取元素
getAttribute() 获取元素属性
setAttribute() 设置元素属性
childNodes() 访问子节点
parentNode() 访问父节点
createElement() 创建元素节点
createTextNode() 创建文本节点
insertBefore() 插入节点
removeChild() 删除节点
offsetHeight() 网页尺寸
scrollHeight() 网页尺寸html>
Hello
Hello
Hello
Hello
Hello
aid2
123
div的p元素
functiongetName(){
varcount=document.getElementsByTagName("p");
alert(count.length);
varp=count[0];
p.innerHTML="World";
}
functiongetAttr(){
varanode=document.getElementById("aid");
varattr=anode.getAttribute("title");
alert(attr);
}
functionsetAttr(){
varanode=document.getElementById("aid2");
anode.setAttribute("title","动态设置a的title属性");
varattr=anode.getAttribute("title");
alert(attr);
}
functiongetChildNode(){
varchildnode=document.getElementsByTagName("ul")[0].childNodes;
alert(childnode.length);
alert(childnode[0].nodeType);
alert(childnode[1].nodeType);
}
functiongetParentNode(){
vardiv=document.getElementById("pid");
alert(div.parentNode.nodeName);
}
functioncreateNode(){
varbody=document.body;
varinput=document.createElement("input");
input.type="button";
input.value="按钮";
body.appendChild(input);
}
functionaddNode(){
vardiv=document.getElementById("div");
varnode=document.getElementById("pid");
varnewNode=document.createElement("p");
newNode.innerHTML="动态添加第一个p元素";
div.insertBefore(newNode,node);
}
functionremoveNode(){
vardiv=document.getElementById("div");
varp=div.removeChild(div.childNodes[1]);
}
functiongetSize(){
varwidth=document.body.offsetWidth||document.documentElement.offsetWidth;
varheight=document.body.offsetHeight||document.documentElement.offsetHeight;
alert(width+","+height);
}
getSize();