好吧,我正在创建一个游戏来娱乐。该游戏包含 5 秒计时器和形状像盒子的 div。第一轮您从 1 个 div 开始,目的是让您在计时器耗尽之前单击该 div。如果您通过,您将进入下一个级别,该级别会添加双倍的盒子,因此第 2 级您有 2 个盒子,第 3 级您有 4 个盒子,依此类推。我在创建总是双倍的 div 长度时遇到问题。我没有太多代码,因为我尝试过的任何方法都不起作用。这是 jsfiddle:
<!doctype html>
<html>
<head>
<title>jQuery Project: An exploding game</title>
<meta charset="utf-8">
<style>
body, html {
width: 960;
height: 500%;
}
div.box {
position: relative;
width: 100px;
height: 100px;
background-color: orange;
}
div.exploding {
position: absolute;
width: 100px;
height: 100px;
background-color: red;
}
</style>
</head>
<body>
</body>
</html>
最佳答案
你的 JavaScript...
setTimeout(function(){$("body").append("<div class='box'></div>").length() *2;}, 5000);
是说...
After 5 seconds get
<body/>
and append a<div class='box'/>
to it, get the number of divs we just appended (i.e. 1) and multiply that number by 2 then discard the result.
使用这个...
setInterval(function(){$(".box").clone().appendTo("body");}, 5000);
它说...
Every 5 seconds, get all the elements of class box, make a copy of them, and append the copy to body.
关于jquery - 添加双倍我已有的 div 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18110119/