1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > dom控制html元素编号 JavaScript DOM对象控制HTML元素详解

dom控制html元素编号 JavaScript DOM对象控制HTML元素详解

时间:2023-10-02 11:54:33

相关推荐

dom控制html元素编号 JavaScript DOM对象控制HTML元素详解

一、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();

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。