简体   繁体   中英

changing color of multiple divs one after another

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')

All the divs i want to change have the class 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:

    create(function(){
        $('.feld').each(function(){ 
            $(this).delay((delay++)*500).css("background-color","lightgoldenrodyellow");
    });
});

doesn't work either

here is the 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>
.delay(duration).css({})

This doesn't apply the css after the duration, but immediately.

You need to use .queue , like in this example :

$('.feld').each(function(){ 
    $(this).delay((delay++)*500).queue(function(next){
        $(this).css("background-color","lightgoldenrodyellow");
        next();
    });
});

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:

$(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);
    });
});

fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM