一、DOM
1. 基本概念
- 文档对象模型(Document Object Model), 是w3c组织推荐的,处理html的标准编程接口
- 可以改变html中结构,内容,样式等
2. DOM 树
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/46af097847ce44b8fa241afe69879a02.jpeg)
3. 获取元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="first">Hello, JS</div>
<ul>
<li>hello01</li>
<li>hello02</li>
<li>hello03</li>
</ul>
<div class="box">box01</div>
<div class="box">box02</div>
<script>
let body = document.body;
console.log(body);
let html = document.documentElement;
console.log(html)
let element = document.getElementById('first');
console.log(element)
console.dir(element);
let list = document.getElementsByTagName('li');
console.log(list[0]);
console.log(list);
let boxList = document.getElementsByClassName('box');
console.log(boxList)
let firstBox = document.querySelector('.box');
let allBox = document.querySelectorAll('.box');
</script>
</body>
</html>
4. 事件
- 事件源
- 事件类型:鼠标点击,鼠标抬起, 鼠标经过
- 事件处理类型
二、 常见操作
1. 修改元素内容
- innerText
- innerHtml: 保留内容和空格
- 可读可写的标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<h3>匿名</h3>
<button>起名字</button>
</div>
<script>
let name = document.querySelector('h3');
let btn = document.querySelector('button');
btn.onclick = function () {
name.innerHTML = '<strong>Erick</strong>';
}
console.log(btn.innerText)
</script>
</body>
</html>
2. 修改元素属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button id="firstBtn">Girl-1</button>
<button id="secondBtn">Girl-2</button><br>
<img src="1.jpg" alt="unable to load..." width="300px">
<script>
let firstButton = document.getElementById('firstBtn');
let secondButton = document.getElementById('secondBtn');
let image = document.querySelector('img');
firstButton.onclick = function changeToFirst() {
image.src = '1.jpg';
};
secondButton.onclick = function changeToSecond() {
image.src = '2.jpg';
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button>确认</button>
<br>
<input type="text" placeholder="请输入">
<script>
let button = document.querySelector('button');
let input = document.querySelector('input');
button.onclick = function () {
input.placeholder = "I am Erick";
this.disabled = true;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button>切换</button><br>
密码:<input type="text">
<script>
let isVisible = true;
let button = document.querySelector('button');
let input = document.querySelector('input');
button.onclick = function changeType() {
isVisible = !isVisible;
if (isVisible === false) {
input.type = 'password';
} else {
input.type = 'text';
}
}
</script>
</body>
</html>
3. 修改元素样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
background-color: red;
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<button>切换样式</button>
<div></div>
<script>
let button = document.querySelector('button');
let div = document.querySelector('div');
button.onclick = function (){
div.style.backgroundColor = 'black';
}
</script>
</body>
</html>