什么是DOM
Document Object Model 文档对象模型,可以理解为代表网页文档的一颗树(模型)。document对象是树根,html,head,body都是树节点。在DOM中一切都是节点(或者元素),元素、节点表示的就是标签对象。
DOM用来干什么
- 找对象(元素节点)(获取DOM)
- 设置标签的属性值
- 设置标签的样式
- 设置标签的值
- 动态创建元素和删除元素
- 事件的触发响应:事件源、事件、事件的驱动程序
获取DOM对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="box" class="box"></div>
<div id="box2" class="box"></div>
<script type="text/javascript">
// 获取文档对象
console.log(document);
// 获取html
console.dir(document.documentElement);
// 获取body
console.log(document.body);
// 获取body中元素节点对象的三种方式
// 通过 id 获取
var oDiv = document.getElementById('box')
console.log(oDiv)
// 通过类名获取
var oDivs = document.getElementsByClassName('box')
console.log(oDivs)
// 通过标签名获取
var oDivs2 = document.getElementsByTagName('div')
console.log(oDivs2)
// 获取第二个div节点
console.log(oDivs2[1])
</script>
</body>
</html>
事件
常用事件
鼠标事件
onclick:点击某个对象时触发
ondblclick:双击某个对象时触发
onmouseover:鼠标移入某个元素时触发
onmouseout:鼠标移出某个元素时触发
onmouseenter:鼠标进入某个元素时触发
onmouseleave:鼠标离开某个元素时触发
onmousedown:鼠标按下时触发
onmouseup:鼠标抬起时触发
onmousemove:鼠标被移动时触发
onwheel:鼠标滚轮滚动时触发
oncontextmenu:点击鼠标右键时触发
键盘事件
onkeydown:键盘的键按下时触发
onkeyup:键盘的键放开时触发
onkeypress:按下或按住键盘键时触发
框架/对象事件
onresize:浏览器窗口大小改变时触发
onabort:图形的加载被中断时触发
onload:元素加载完时触发
onerror:当加载文档或者图片时(没找到)发生的错误时触发
onscroll:文档滚动时触发
onpageshow:用户访问页面时触发
onunload:用户退出页面时触发
表单事件
onfocus:元素获得焦点时触发
onblur:元素失去焦点时触发
onchange:元素内容改变时触发
oninput:元素获取用户输入时触发
onsubmit:提交按钮时触发
onreset:重置按钮时触发
onselect:文本被选中时触发
拖动事件
ondrag:元素正在拖动时触发
ondragend:用户完成元素拖动时触发
多媒体事件
onplay:在视频/音频开始播放时触发
onended:在视频/音频播放结束时触发
onpause:在视频/音频暂停时触发
绑定事件
直接绑定匿名函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box{
width:100px;
height:100px;
background-color:green;
}
</style>
</head>
<body>
<div id="box" class="box"></div>
<script>
// onload 入口函数为window.onload()
// window.onload特点:1.等待文档和图片资源加载完成之后,会触发onload事件 2.window.onload()有事件覆盖现象,后写的覆盖前面写的
window.onload = function() {
// 1.获取事件源
var oDiv = document.getElementById('box');
// 事件
oDiv.onclick = function(){
// 3.事件驱动
alert(1);
};
};
</script>
</body>
</html>
效果:
节点样式属性操作
点击红色标签后,div标签颜色更改,此处更改的原因是,行内样式的优先级大于内接样式。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box{
width:100px;
height:100px;
background-color:red;
}
</style>
</head>
<body>
<div class="box"></div>
<script>
var oDiv = document.getElementByClass('box');
oDiv.onclick = function() {
// 获取的是行内样式的值,与内接和外接样式没有任何关系
console.log(oDiv.style.backgroundColor) // 行内样式没有设定background-color,所以获取不到。
oDiv.style.backgroundColor = 'green';
console.log(oDiv.style.backgroundColor)
}
</script>
</body>
</html>
节点(标签)值的操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box{
width:100px;
height:100px;
background-color:red;
}
</style>
</head>
<body>
<button id="btn">设置文本</button>
<div id="box" class="box">
wu
<h2>hh</h2>
</div>
<input type="text" value="123" id="name">
<script type="text/javascript">
window.onload = function(){
/* 使用函数简化代码,解决冗余
var oBtn = document.getElementById('btn');
var oDiv = document.getElementById('box');
var oInput = document.getElementById('name');
*/
function $(id){
return document.getElementById(id);
}
// oBtn.onclick = function (){
$('btn').onclick = function (){
console.log($('box').innerText); // 只获取文本
console.log($('box').innerHTML); // 获取当前节点下的所有节点(文本、标签、换行),既有文本又有标签
// 修改div标签之间的文本内容的设定
$('box').innerText = 'hello';
/*单闭合标签修改文本值,即修改其value属性值*/
console.log($('name').value) // 有value属性的标签
$('name').value = '456';
}
}
</script>
</body>
</html>
节点属性值的操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box{
background-image:url(./icon-slides.png);
width:41px;
height:69px;
}
</style>
</head>
<body>
<a href=""><img src="pre1.PNG" alt="上一张" id="pic"></a>
<script>
window.onload = function(){
oImg = document.getElementById('pic')
oImg.onmouseover = function(){
// console.log(oImg)
// 好比是python中的self,谁调用的事件this就值得就是谁
console.log(this) // this指oImg节点对象,因为是oImg绑定事件
console.log(this.getAttribute('src')) // 获取相对路径
console.log(this.src) // 获取的是绝对路径
// 修改属性
this.setAttribute('src', 'pre2.PNG')
//修改属性的简便方式,建议使用。
// this.src = 'pre2.PNG'
//修改class的值时特殊
// this.className = 'c1 '
}
oImg.onmouseout = function(){
this.setAttribute('src', 'pre1.PNG')
}
}
</script>
</body>
</html>
效果:
DOM节点操作
新节点的父子插入、兄弟插入。创建新节点,删除新节点,查找节点的父节点,查找节点的儿子节点。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
</style>
</head>
<body>
<button id="create">创建</button>
<button id="destroy">移除</button>
<div id="box" class="box">
<p id="hh">哈哈哈</p>
</div>
<script>
window.onload = function(){
function $(id){
return document.getElementById(id);
}
var oP = null;
// 创建-->销毁,页面性能会有损耗。如果页面中出现频繁的切换,不要使用这种方式。建议通过控制类的样式属性(隐藏/显示)来进行切换。
$('create').onclick = function(){
// 创建p节点元素
oP = document.createElement('p');
// 设置文本内容
oP.innerText = 'hello';
//追加元素, 父元素.appendChild(子元素)
$('box').appendChild(oP);
// 兄弟之间插入新节点。父元素.insertBefore(新的子节点,作为参考的节点)
// $('box').insertBefore(oP, $('hh'))
/*
获取父节点:节点对象.parentNode
获取子节点(亲儿子)集合:节点对象.children()
*/
}
$('destroy').onclick = function(){
//父元素.removeChild(子节点)
$('box').removeChild(oP)
}
}
</script>
</body>
</html>