【发布时间】:2017-06-03 19:08:00
【问题描述】:
我正在尝试用 javascript 编写一段代码,在随机位置显示一个 div,然后在 1000 毫秒后将其涂黑并等待用户点击。我的问题是使它变黑(以//开头的行)。你能解释一下这有什么问题吗?
<style type="text/css">
#test {
position:absolute;
width:40px;
height:40px;
background-color:#d2fcd9;
}
</style>
<title></title>
<script type="text/javascript">//<![CDATA[
$(function(){
$('#test').click(function() {
var docHeight = $(document).height(),
docWidth = $(document).width(),
$div = $('#test'),
divWidth = $div.width(),
divHeight = $div.height(),
heightMax = docHeight - divHeight,
widthMax = docWidth - divWidth;
$div.css({
left: Math.floor( Math.random() * widthMax ),
top: Math.floor( Math.random() * heightMax ),
});
//setTimeout(function() { $div.css({background-color: '#000000' }); }, 300);
});
});//]]>
</script>
</head>
<body bgcolor="#000000">
<div id="test" style="left: 1168px; top: 222px;"></div>
【问题讨论】:
-
在 "background-color" 上尝试双引号 setTimeout(function() { $div.css({background-color: '#000000' }); }, 300);
-
谢谢!这行得通。但我无法更改下一轮的背景颜色 - 单击 div 后,我想以原始颜色再次查看它。
-
检查我的答案
-
我试过了。正如我写的那样,它在第一轮之后就可以工作 - 如果我点击现在黑色的 div 什么都不会发生。
标签: javascript html random