【发布时间】:2021-03-20 14:46:39
【问题描述】:
我不知道为什么这段代码不起作用!为什么它显示警报空白?
<html lang="en">
<head>
<title>Document</title>
<style>
#dd{
height: 300px;
width: 300px;
background-color: mediumblue;
color: white;
}
</style>
</head>
<body>
<label>Color </label><input id="in1" type="text">
<hr>
<div id="dd">
</div>
<script>
var colorTextBox = document.getElementById("in1");
var div = document.getElementById("dd");
div.onclick = function(){
colorTextBox.value = div.style.backgroundColor;
alert(div.style.backgroundColor);
}
</script>
</body>
</html>
当点击 div 时,div 的背景颜色应该写在输入框内,并且也会显示在 alert 中。
【问题讨论】:
标签: javascript html