如下图,通过点击按钮,在div盒子中插入文字,并背景色
1、可以使用div中的innerText属性在div中插入文本内容,style.backgroudColor属性设置背景色。
2、完整代码
<html>
<head>
<meta charset = "utf-8">
<title>改变div中的文字和背景色</title>
</head>
<body>
<div id="container" style="background-color:#c6efbf;width:100px;height:200px;"></div>
<button type="button" onclick="InsertText()">插入文字</button>
</body>
<script>
function InsertText(){
var div = document.getElementById("container");
div.innerText ="我是插入的文字";
div.style.backgroundColor="#f5f6f7";
}
</script>
</html>