DOM是document object model的缩写 文档对象模型
DOM是关于如何获取、修改、添加和删除HTML元素的标准
DOM是由节点组成的
节点的关系:父子兄弟
1、获取节点的方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">app</div>
<ul>
<li class="item">1</li>
<li class="item">2</li>
<li class="item">3</li>
<li class="item">4</li>
</ul>
<script>
var $app=document.querySelector('#app');
console.log($app);
var $item=document.querySelector('.item');
console.log($item);
var $items=document.querySelectorAll('.item');
console.log($items);
</script>
</body>
</html>
2、获取样式和属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
a{background-color: black;}
</style>
</head>
<body>
<a href="https://www.qq.com" style="font-size: 20px;color: aqua;">QQ</a>
<script>
//获取a的href属性
var $a = document.querySelector('a');
console.log($a.href);
//获取a的样式
$a.style.color = 'red';
console.log('a的样式:', $a.style.fontSize, $a.style.color);
console.log('');
</script>
</body>
</html>
3、获取文本和标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<div>
aaaaaa
<p>
bbbbbb
</p>
</div>
</div>
<script>
var $app = document.querySelector('#app');
console.log($app.innerHTML);
console.log($app.innerText);
</script>
</body>
</html>
4、添加节点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ul id="list">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<script>
//创建节点
var $li=document.createElement('li');
$li.innerText=4;
//把li节点插入到ul里面
var $list = document.querySelector('#list')
$list.appendChild($li);
</script>
</body>
</html>
5、通过事件添加节点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button onclick="add()">添加节点</button>
<ul id="list">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<script>
var num = 4;
function add() {
//创建节点
var $li = document.createElement('li');
$li.innerText = num;
$li.innerText = num++;
//把li节点插入到ul里面
var $list = document.querySelector('#list')
$list.appendChild($li);
}
</script>
</body>
</html>
6、删除节点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button onclick="remove()">删除节点</button>
<ul id="list">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<script>
var num = 4;
function remove() {
var $list = document.querySelector('#list');
$list.remove();
}
</script>
</body>
</html>
7、修改节点样式属性(1)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
border: 1px solid;
margin-top: 20px;
}
</style>
</head>
<body>
<button onclick="showDiv()">显示</button>
<div style="display: none;" class="box">
<p>xxxxxx</p>
<p>xxxxxx</p>
<p>xxxxxx</p>
</div>
<script>
function showDiv() {
var $box=document.querySelector('.box');
var $button=document.querySelector('button');
var flag = $box.style.display;
if(flag=='none'){
$box.style.display='block';
$button.innerText='隐藏';
}else {
$box.style.display='none';
$button.innerText='显示';
}
}
</script>
</body>
</html>
8、修改节点样式属性(2)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
border: 1px solid;
margin-top: 20px;
}
.dsn {
display: none;
}
</style>
</head>
<body>
<button onclick="showDiv()">显示</button>
<div class="box">
<p>xxxxxx</p>
<p>xxxxxx</p>
<p>xxxxxx</p>
</div>
<script>
function showDiv() {
var $box = document.querySelector('.box');
var $button = document.querySelector('button');
var className = $box.className;
if (className == 'box') {
$box.className = 'box dsn';
$button.innerText = '隐藏';
} else {
$box.className = 'box';
$button.innerText = '显示';
}
}
</script>
</body>
</html>
9、修改节点元素属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 300px;
height: 300px;
}
</style>
</head>
<body>
<button onclick="changeImg(1)">换图1</button>
<button onclick="changeImg(2)">换图2</button>
<div class="box">
<img src="./img/fj1.jpg" alt="">
</div>
<script>
function changeImg(num){
document.querySelector('img').src=`./img/fj${num}.jpg`;
}
</script>
</body>
</html>