【发布时间】:2014-08-23 16:20:15
【问题描述】:
我正在处理this demo。为什么我无法为动态生成的 div 添加边距?
jQuery(function () {
var rnd1 = Math.ceil(Math.random() * 450);
var rnd2 = Math.ceil(Math.random() * 450);
for (var i = 0; i < 6; i++) {
$('.wrapper').append('<div class="box"></div>');
// $('.box').css({"left": rnd1,"top": rnd2});
}
$(".box").each(function () {
$(this).css({
"left": rnd1,
"top": rnd2
});
});
如你所见,我两种都试过了:
$('.box').css({"left": rnd1,"top": rnd2});
在循环内部并使用.each() 作为:
$(".box").each(function () {
$(this).css({
"left": rnd1,
"top": rnd2
});
});
在循环之外,但显然他们没有做这项工作!
【问题讨论】:
-
为什么不在
.each()循环中使用所需的CSS 属性和.toggleClass("someClass")定义.someClass? -
@royhowie - 回答“你可以使用
live...”的问题并不是真正的重复,只是错误。
标签: javascript jquery css