i have a problem changing the color of some divs one by one. on page load i create a div which is filled with a square of 15x15 divs (class='feld')
我有一个问题一个一个地改变一些div的颜色。在页面加载我创建一个div,其中填充了一个15x15 div的正方形(class ='feld')
All the divs i want to change have the class feld
我想改变的所有div都有类feld
My code so far:
我的代码到目前为止:
$(document).ready(function() {
create();
var delay = 1;
function create(){
for (z = 1; z < 16; z++) {
var zeile = jQuery('<div/>', {
class: 'zeile',
id: z,
html: ""
});
$("#wortfeld").append(zeile);
for (s = 1; s < 16; s++) {
var div = jQuery('<div/>', {
class: 'feld',
id: s,
html: ""
});
$(".zeile[id=" + z + "]").append(div);
};
};
};
$('.feld').each(function(){
$(this).delay((delay++)*500).css("background-color","lightgoldenrodyellow");
});
});
So my intention was to change the color of each .feld one by one. But it doesn't work the way i approached this. I also tried to do it this way:
所以我的目的是逐个改变每个.feld的颜色。但它不能像我接近这个那样工作。我也试过这样做:
create(function(){
$('.feld').each(function(){
$(this).delay((delay++)*500).css("background-color","lightgoldenrodyellow");
});
});
doesn't work either
也不起作用
here is the HTML
这是HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="de">
<head>
<link href="design_crossword.css" type="text/css" rel="stylesheet">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
<script type="text/javascript" src="crossword.js"></script>
</head>
<body>
<div id="wrapper">
<div id="wortfeld">TEST</div>
</div>
</body>
</html>
2 个解决方案
#1
0
Firstly, an unrelated issue but one that could cause problems: You shouldn't use an ID more than once, give your zeile
and feld
items unique ID's. You could also get some mileage with setTimeout, no need to involve .delay:
首先,一个不相关的问题,但可能会导致问题:你不应该多次使用一个ID,给你的zeile和feld项目唯一的ID。您还可以使用setTimeout获得一些里程,无需涉及.delay:
$(document).ready(function() {
create();
var delay = 1;
function create(){
for (z = 1; z < 16; z++) {
var zeile = jQuery('<div/>', {
class: 'zeile',
id: 'z'+z,
html: ""
});
$("#wortfeld").append(zeile);
for (s = 1; s < 16; s++) {
var div = jQuery('<div/>', {
class: 'feld',
id: 'z'+z+'s'+s,
html: ""
});
$("#z" + z).append(div);
};
};
};
$('.feld').each(function(){
setTimeout(function(x){
$(x).css("background-color","lightgoldenrodyellow");
},(delay++)*500,this);
});
});
#2
0
.delay(duration).css({})
This doesn't apply the css after the duration, but immediately.
这不会在持续时间之后应用css,而是立即应用。
You need to use .queue
, like in this example:
你需要使用.queue,就像在这个例子中一样:
$('.feld').each(function(){
$(this).delay((delay++)*500).queue(function(next){
$(this).css("background-color","lightgoldenrodyellow");
next();
});
});
#1
0
Firstly, an unrelated issue but one that could cause problems: You shouldn't use an ID more than once, give your zeile
and feld
items unique ID's. You could also get some mileage with setTimeout, no need to involve .delay:
首先,一个不相关的问题,但可能会导致问题:你不应该多次使用一个ID,给你的zeile和feld项目唯一的ID。您还可以使用setTimeout获得一些里程,无需涉及.delay:
$(document).ready(function() {
create();
var delay = 1;
function create(){
for (z = 1; z < 16; z++) {
var zeile = jQuery('<div/>', {
class: 'zeile',
id: 'z'+z,
html: ""
});
$("#wortfeld").append(zeile);
for (s = 1; s < 16; s++) {
var div = jQuery('<div/>', {
class: 'feld',
id: 'z'+z+'s'+s,
html: ""
});
$("#z" + z).append(div);
};
};
};
$('.feld').each(function(){
setTimeout(function(x){
$(x).css("background-color","lightgoldenrodyellow");
},(delay++)*500,this);
});
});
#2
0
.delay(duration).css({})
This doesn't apply the css after the duration, but immediately.
这不会在持续时间之后应用css,而是立即应用。
You need to use .queue
, like in this example:
你需要使用.queue,就像在这个例子中一样:
$('.feld').each(function(){
$(this).delay((delay++)*500).queue(function(next){
$(this).css("background-color","lightgoldenrodyellow");
next();
});
});