jquery - 添加双倍我已有的 div 时遇到问题

标签 jquery html

好吧,我正在创建一个游戏来娱乐。该游戏包含 5 秒计时器和形状像盒子的 div。第一轮您从 1 个 div 开始,目的是让您在计时器耗尽之前单击该 div。如果您通过,您将进入下一个级别,该级别会添加双倍的盒子,因此第 2 级您有 2 个盒子,第 3 级您有 4 个盒子,依此类推。我在创建总是双倍的 div 长度时遇到问题。我没有太多代码,因为我尝试过的任何方法都不起作用。这是 jsfiddle:

http://jsfiddle.net/JZdkQ/

<!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.

http://jsfiddle.net/JZdkQ/2/

关于jquery - 添加双倍我已有的 div 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18110119/

相关文章:

html - 在特定断点处配置 .container max-width - Tailwindcss

javascript - 如何修改指令中 ng-repeat 创建的每个元素的单独范围?

html - 为什么我的表单单独显示很好,但放在整个页面时却乱七八糟?

javascript - 可以用 javascript 更改输入标签的名称吗?

php - 访问 json 对象中的数字属性

java - 如何为 ImageSpan 设置背景颜色和下划线

javascript - 如何使用 Bootstrap 3 在模态内设置样式

javascript - $.each 函数未按预期工作

javascript - jQuery mousedown 事件不会在 IE8 中为窗口触发

jquery - 如何使用 [attribute=value] 选择器?