Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
添加了没有显示javascript的div_Javascript_Html - Fatal编程技术网

添加了没有显示javascript的div

添加了没有显示javascript的div,javascript,html,Javascript,Html,我正在尝试使用javascript添加一个div。div出现在代码和chromes开发工具中,但没有显示在实际窗口中 为什么会发生这种情况,我该如何纠正 代码: 在这里潜水 函数add(){ var inner=document.getElementById('inner'); var div=document.createElement('div'); div.style.height='300px'; div.style.width='100px'; div.style.color='蓝色

我正在尝试使用javascript添加一个div。div出现在代码和chromes开发工具中,但没有显示在实际窗口中

为什么会发生这种情况,我该如何纠正

代码:


在这里潜水
函数add(){
var inner=document.getElementById('inner');
var div=document.createElement('div');
div.style.height='300px';
div.style.width='100px';
div.style.color='蓝色';
内部子对象(div);
}
添加();

您已设置文本
颜色
,而不是背景色。它就在那里,只是它没有内容,所以你看不见。我想你的意思是:

div.style.backgroundColor = 'blue';

您已设置文本
颜色
,而不是背景色。它就在那里,只是它没有内容,所以你看不见。我想你的意思是:

div.style.backgroundColor = 'blue';

试试这个代码,它对我有用

您必须在body标记中添加onload()

<html>
<head>
<script>
function add() {

var inner = document.getElementById('inner');
var div = document.createElement("div");
div.style.width = "300px";
div.style.height = "100px";
div.style.background = "blue";


inner.appendChild(div);
}

</script>
</head>

<body onload ="add()">

<div id="inner"></div>

</body>
</html>

函数add(){
var inner=document.getElementById('inner');
var div=document.createElement(“div”);
div.style.width=“300px”;
div.style.height=“100px”;
div.style.background=“蓝色”;
内部子对象(div);
}

试试这个代码,它对我有用

您必须在body标记中添加onload()

<html>
<head>
<script>
function add() {

var inner = document.getElementById('inner');
var div = document.createElement("div");
div.style.width = "300px";
div.style.height = "100px";
div.style.background = "blue";


inner.appendChild(div);
}

</script>
</head>

<body onload ="add()">

<div id="inner"></div>

</body>
</html>

函数add(){
var inner=document.getElementById('inner');
var div=document.createElement(“div”);
div.style.width=“300px”;
div.style.height=“100px”;
div.style.background=“蓝色”;
内部子对象(div);
}

基于这一点,我建议OP使用inspector工具查看DOM以捕获此类内容。基于这一点,我建议OP使用inspector工具查看DOM以捕获此类内容。颜色用于文本,而不是背景。请参见颜色用于文本,而不是背景。看见