内容操作 | 描述 |
---|---|
innerText | 从开始位置到结束位置 去除标签、空格、换行 |
innerHTML | 从开始位置到结束位置 包括标签、空格、换行 |
<!DOCTYPE html>
<html lang="zh-CN">
<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>元素操作</title>
<style>
div {
font-size: 24px;
line-height: 30px;
text-align: center;
color: black;
background-color: white;
}
.border {
border: 5px solid red;
}
</style>
</head>
<body>
<div>时间:<span></span></div>
<button id="date">获取时间</button>
<button id="color">颜色切换</button>
<button id="border">边框</button>
<script>
const btn = document.getElementById('date')
const color = document.getElementById('color')
const border = document.getElementById('border')
const div = document.querySelector('div')
const span = document.querySelector('span')
date.onclick = function () {
span.innerText = new Date()
}
color.onclick = () => {
div.style.color = div.style.color === 'white' ? 'black' : 'white'
div.style.backgroundColor =
div.style.backgroundColor === 'black' ? 'white' : 'black'
}
border.onclick = () => {
div.className = div.className === 'border' ? '' : 'border'
}
</script>
</body>
</html>