DOM是文档对象模型;具有赋予js操作节点的能力。
A、查找元素:
1、通过 id 找到元素
var oBtn=document.getElementById("btn");
var oBtn=document.getElementById("btn");
var aText=oBtn.getElementsByTagName("text");
var oUl=document.getElementsByClassName("li");
注意:通过类名获取元素在IE6-8不兼容
B、DOM节点
节点分成两种:文本节点,元素节点。
1.1、childNodes:可找到的是文本节点与元素节点;这时可用nodeType来判断节点类型,然后再控制子节点。
例子如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>childNodes与nodeType的使用</title>
<script type="text/javascript">
window.οnlοad=function(){
var oList=document.getElementById('list');
var num=0;
for(var i=0;i<oList.childNodes.length;i++){
//nodeType==3 是文本节点
//nodeType==1 是元素节点
if(oList.childNodes[i].nodeType==1){
num++;
oList.childNodes[i].style.background='red';
}
}
console.log(num);
}
</script>
</head>
<body>
1.2、children:也可以获取子节点(元素节点),而且兼容各种浏览器。包括IE6-8,因此较常用。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>children的使用</title>
<script type="text/javascript">
window.οnlοad=function(){
var oList=document.getElementById('list');
var ochild=document.getElementById('child');
//oList.children 父节点.子节点
//console.log(oList.children.length);
//通过子节点查找父节点的方式
console.log(ochild.parentNode);
}
</script>
</head>
<body>
<ul id="list">
<li id="child"></li>
<li></li>
</ul>
</body>
</html>
2.1、获取父节点:parentNode
<!DOCTYPE html><html>
<head>
<meta charset="UTF-8">
<title>parentNode的使用</title>
<script type="text/javascript">
window.οnlοad=function(){
var oUl=document.getElementById('list');
var aBtn=oUl.getElementsByTagName('a');
for(var i=0;i<aBtn.length;i++){
aBtn[i].οnclick=function(){
this.parentNode.style.display='none';
}
}
}
</script>
</head>
<body>
<ul id="list">
<li>恭喜发财<a href="javascript:">删除</a></li>
<li>恭喜发财<a href="javascript:">删除</a></li>
<li>恭喜发财<a href="javascript:">删除</a></li>
<li>恭喜发财<a href="javascript:">删除</a></li>
</ul>
</body>
</html>
3、offsetParent:绝对相对定位后的父节点。没有相对定位盒子,则父节点是body。
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1 {
width: 200px;
height: 200px;
background: #CCC;
margin: 100px;
}
#div2 {
width: 100px;
height: 100px;
background: red;
position: absolute;
left: 50px;
top: 50px;
}
</style>
<script>
window.onload = function() {
var oDiv2 = document.getElementById('div2');
alert(oDiv2.offsetParent);
};
</script>
</head>
<body>
<div id="div1">
<div id="div2"></div>
</div>
</body>
</html>
4、首尾子节点
4.1、首部子节点:
firstElementChild;其他浏览器;
firstChild:IE浏览器;
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script>
window.onload = function() {
var oUl = document.getElementById('ul1');
if (oUl.firstElementChild) {
oUl.firstElementChild.style.background = 'red'; //非IE6-8浏览器
} else {
oUl.firstChild.style.background = 'red'; //IE6-8
}
};
</script>
</head>
<body>
<ul id="ul1">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</body>
</html>
4.2、尾部子节点
lastElementChild:其他浏览器;
lastChild:IE浏览器;
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script>
window.onload = function() {
function lastElementChild(ele){
if(ele.lastElementChild){
return ele.lastElementChild;//其他
}
else{
return ele.lastChild;//IE
}
}
lastElementChild(oList).style.background='red';
}
</script>
</head>
<body>
<ul id="ul1">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</body>
</html>
C图片无缝滚动
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; } ul,li{ list-style: none; } #box{ height: 464px; width: 185px; border: 1px solid #000; margin: 100px; position: relative; overflow: hidden; } #box ul{ position: absolute; left: 0; top: 0; } #box ul li{ float: left; width: 185px; height: 116px; } #box ul li img{ display: block; width: 185px; height: 116px; } #direction p{ width: 100px; height: 40px; line-height: 40px; text-align: center; background: green; color: #fff; float: left; margin-right: 5px; } </style> <script type="text/javascript"> window.οnlοad=function(){ var oBox=document.getElementById('box'); var oUl=oBox.getElementsByTagName('ul')[0]; var aLi=oUl.getElementsByTagName('li'); var oTotop=document.getElementById('to_top'); var oTobottom=document.getElementById('to_bottom'); var speed=-2; oUl.style.height=aLi[0].offsetHeight*aLi.length+'px'; oUl.innerHTML=oUl.innerHTML+oUl.innerHTML; function totop(){ if(oUl.offsetTop<-oUl.offsetHeight/2){ oUl.style.top='0'; } if(oUl.style.top>0){ oUl.style.top=-oUl.offsetHeight/2+'px'; } oUl.style.top=oUl.offsetTop+speed+'px'; } oTotop.οnclick=function(){ speed=-2; } oTobottom.οnclick=function(){ speed=2; } setInterval(totop,50); } </script> </head> <body> <div id="box"> <ul> <li><img src="img/a1.jpeg" alt="" /></li> <li><img src="img/a2.jpeg" alt="" /></li> <li><img src="img/a3.jpeg" alt="" /></li> <li><img src="img/a4.jpeg" alt="" /></li> </ul> </div> <div id="direction"> <p id="to_top">向上</p> <p id="to_bottom">向下</p> </div> </body> </html>