用JS插入一个DIV怎么写???这样写有错吗?
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>script</title><script>...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>script</title>
<script>
var box1 = document.getElementById("box1");
var box= document.createElement("div");
document.box1.appendChild(box)
</script>
</head>
<body>
<div id="box1"></div>
</body>
</html> 展开
<html lang="en">
<head>
<meta charset="UTF-8">
<title>script</title>
<script>
var box1 = document.getElementById("box1");
var box= document.createElement("div");
document.box1.appendChild(box)
</script>
</head>
<body>
<div id="box1"></div>
</body>
</html> 展开
1个回答
展开全部
这样写:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>script</title>
</head>
<body>
<div id="box1"></div>
</body>
</html>
<script>
//放在最后,执行的时候才能获取到 box1
var box1 = document.getElementById("box1");
var box= document.createElement("div");
//box1 已经定义为document下的元素了,不需要再重复
box1.appendChild(box)
//document.box1.appendChild(box)
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>script</title>
</head>
<body>
<div id="box1"></div>
</body>
</html>
<script>
//放在最后,执行的时候才能获取到 box1
var box1 = document.getElementById("box1");
var box= document.createElement("div");
//box1 已经定义为document下的元素了,不需要再重复
box1.appendChild(box)
//document.box1.appendChild(box)
</script>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询