Js_dom_01

来源:互联网 发布:centos apache php 编辑:程序博客网 时间:2024/06/16 19:18

该文章是学习妙味视频课程后的学习笔记整理 方便自己日后复习所用

文档对象模型(Document Object Model,DOM

Dom 是Js的一个组成部件也是w3c的一套标准,我们通过js对网页的操作都是通过dom,并且dom是属于浏览器的而不是属于JacaScript的。

说白了dom就是帮助js来操作html页面里的元素而html中的元素又称为节点,而根据类型不同又可以称为不同的节点比如文本节点,元素节点,属性节点等等。

childNodes nodeType
childNodes用来获取当前节点下的所有子节点但是这个有兼容性问题
比如下面这个例子 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><script type="text/javascript">window.onload=function (){var oUl=document.getElementById('ul1');    //ul节点下的子节点长度在ie下为3个 在chrome下为7个    //因为ie只将元素节点作为oUl的子节点而chrome则将文本节点也作为    //oUl的子节点alert(oUl.childNodes.length);//oUl.childNodes[0].style.background='red';}</script></head><body><ul id="ul1"><li>111</li><li>222</li><li>333</li></ul></body></html>
nodeType是用来获取节点类型的
1 代表元素节点 3代表文本节点 
alert(document.body.childNodes[0].nodeType);
chidren 相当于兼容版的childNodes 只将元素节点作为子节点
var oUl=document.getElementById('ul1');alert(oUl.children.length); //所有浏览器下显示个数为3  
parentNode 用来获取父元素节点
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><style></style><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript">window.onload=function (){var aA=document.getElementsByTagName('a');var i=0;for(i=0;i<aA.length;i++){aA[i].onclick=function (){this.parentNode.style.display='none';}}}</script></head><body><ul><li>aass <a href="javascript:;">隐藏</a></li><li>5453 <a href="javascript:;">隐藏</a></li><li>cvxc <a href="javascript:;">隐藏</a></li><li>ertert <a href="javascript:;">隐藏</a></li></ul></body></html>
offsetParent 获取html布局中你所参考的节点
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><style></style><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript"></script></head><body><!--position:relative; div1 的 style 加上 相对布局则里面的div则是在布局上参照div1所以this.offsetParent.tagName 为DIV 如果去掉position:relative; 则为body因为内部的div的position是absolute直接参照了body--><div id="div1" style="width:100px; height:100px; background:red; margin:100px; "><div onclick="alert(this.offsetParent.tagName)" id="div2" style="width:100px; height:100px; background:yellow; position:absolute; left:100px; top:100px;"></div></div></body></html>
首尾子节点 有兼容性问题
firstChild、firstElementChild 
lastChild 、lastElementChild
兄弟节点 有兼容性问题
nextSibling、nextElementSibling
previousSibling、previousElementSibling
虽然以上几个都有兼容问题但是可以通过封装函数解决或者
var oFirst=oUl.firstElementChild||oUl.firstChild;
第一种:oDiv.style.display=“block”;
第二种:oDiv.style[“display”]=“block”;
第三种:Dom方式
DOM方式操作元素属性
获取:getAttribute(名称)
设置:setAttribute(名称, 值)
删除:removeAttribute(名称)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><style></style><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript">window.onload=function (){var oTxt=document.getElementById('txt1');//1//oTxt.value='123';//2//oTxt['value']='abc';//oTxt.removeAttribute('value');//3//oTxt.setAttribute('value', 'rtertw');alert(oTxt.getAttribute('id'));}</script></head><body><input type="text" id="txt1"/></body></html>
className 通过用户设定的class来获取元素
var oUl=document.getElementById('ul1');var aLi=oUl.getElementsByTagName('li'); //aLi是一个数组里面有许多dom节点

0 0