1.innerText属性,用于设置或获取元素的文本内容。
<!DOCTYPE html>
<html>
<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>
#box{
width: 200px;
height: 200px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
// 获取box
let box = document.getElementById('box')
// 在box里面添加<h2>哈哈!我是h2</h2>文字内容
box.innerText = '<h2>哈哈!我是h2</h2>'
console.log(box.innerText);
</script>
</body>
</html>
2.innerHTML属性,用于设置或获取元素的HTML内容。
<!DOCTYPE html>
<html>
<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>
#box{
width: 200px;
height: 200px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
// 获取box
let box = document.getElementById('box')
// 在box里面添加 h2 标签
box.innerHTML = '<h2>哈哈!我是h2</h2>'
console.log(box.innerHTML);
</script>
</body>
</html>