DOM到底是什么?
DOM 提供了一种表述形式将文档作为一个结构化的节点组以及包含属性和方法的对象。从本质上说,它将web 页面和脚本或编程语言连接起来了。 (MDN)
直白一点说,就是把文档转换成为节点树,然后告诉大家,这个html的文档上有这些节点对象,对象有这些属性并提供了操作方法,使可以通过脚本语言来操作这些节点对象,从而实现web页面和脚本之间的联系。
核心属性和操作方法参考DOM概述
实例练习
- 当鼠标放置在li元素上,会在img-preview里展示当前li元素的data-img对应的图片。
效果
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>practise</title>
</head>
<body>
<ul class="ct">
<li data-img="http://zzpic1.58cdn.com.cn/zhuanzh/n_v1bl2lwxtwuywvqqddizba.jpg">鼠标放置查看图片1</li>
<li data-img="https://cn.bing.com/th?id=OIP._rZSDSg1oAr_UDd8klCEjAHaHa&pid=Api&rs=1">鼠标放置查看图片2</li>
<li data-img="http://i1.muzisoft.com/uploads/image/20161013/20161013141457_95667.jpg">鼠标放置查看图片3</li>
</ul>
<div class="img-preview"></div>
<script>
var imgElement = document.createElement("img");
var divElement = document.querySelector("div");
var ulElement = document.querySelector("ul");
divElement.appendChild(imgElement);
//事件委托
ulElement.onmouseover = function showPic(e) {
imgElement.src = e.target.getAttribute("data-img");
}
</script>
</body>
</html>
- 当点击每一个元素li时控制台展示该元素的文本内容。
效果
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<ul>
<li>猫猫</li>
<li>狗狗</li>
</ul>
<script>
document.querySelector("ul").onclick=function(e){
console.log(e.target.innerText)
}
</script>
</body>
</html>
- 实现此效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
.clearfix::after {
content: '';
clear: both;
display: block;
}
.container {
margin: 0 auto;
}
#box {
border: 2px solid #ef6571;
padding: 20px 40px;
font-size: 40px;
text-align: center;
display: inline-block;
}
ul {
list-style: none;
margin-top: 10px;
}
a {
text-decoration: none;
color: black;
}
li {
float: left;
margin-right: 10px;
border: 1px solid black;
padding: 3px 6px;
background-color: #ef6571;
}
.bgcolor {
background-color: black;
color: white;
}
</style>
</head>
<body>
<div class="container">
<div id="box">
hello!
</div>
<ul class="clearfix">
<li><a href="#" id="addbtn">add</a></li>
<li><a href="#" id="removebtn">remove</a></li>
<li><a href="#" id="togglebtn">toggle</a></li>
</ul>
</div>
</body>
<script>
var divBox = document.querySelector("#box");
document.querySelector("#addbtn").onclick = function () {
divBox.classList.add("bgcolor");
}
document.querySelector("#removebtn").onclick = function () {
divBox.classList.remove("bgcolor");
}
document.querySelector("#togglebtn").onclick = function () {
divBox.classList.toggle("bgcolor");
}
</script>
</html>
- 实现此效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
ul {
background-color: #14d33d;
display: inline-block;
padding-right: 20px;
}
</style>
</head>
<body>
<ul>
<li>天苍苍</li>
<li>野茫茫</li>
<li>风吹草地</li>
<li>见牛羊</li>
</ul>
<br>
<input type="text">
<input type="button" value="加在开头" id="addStartBtn">
<input type="button" value="加在末尾" id="addEndBtn">
</body>
<script>
var ulList = document.querySelector("ul");
document.querySelector("#addEndBtn").addEventListener("click", function () {
var value = document.querySelector("input").value;
if (value) {
var liChild = document.createElement("li");
liChild.innerText = value;
ulList.appendChild(liChild);
}
});
document.querySelector("#addStartBtn").addEventListener("click", function () {
var value = document.querySelector("input").value;
if (value) {
var liChild = document.createElement("li");
liChild.innerText = value;
ulList.insertBefore(liChild, ulList.firstChild);
}
});
</script>
</html>