使用jquery更新更新时的背景颜色

时间:2012-04-16 17:52:05

标签: jquery

我正在尝试更新元素的背景颜色。

当响应从服务器返回时,我想要将元素的背景颜色更改一秒左右,然后将其更改回原始颜色。我可以更改背景颜色,但我不能在同一个调用中更改它。

这是我到目前为止所做的:

$.each(theArray,function(intIndex,objValue){
    $("#" + objValue.id).parent().css('background-color','red');
});
//NEED TIMING EVENT FOR DELAY 
$.each(theArray,function(intIndex,objValue){
    $("#" + objValue.id).parent().css('background-color','transparent');
});

第一个循环工作正常,但我不能让另一个循环工作。

我试过这种方式:

$.each(theArray,function(intIndex,objValue){
     $("#" + objValue.id).parent.css('background-color','red').delay(1000).css('background-color','transparent');
});

这也没有做任何事情。

所以我不确定问题是什么。有什么建议吗?

4 个答案:

答案 0 :(得分:4)

$.each(theArray,function(intIndex,objValue){
    var $el = $("#" + objValue.id).parent().css('background-color','red');
    setTimeout(function(){
        $el.css('background-color', '');
    }, 1000);
});

以下是演示:http://jsfiddle.net/Wnntu/

答案 1 :(得分:0)

你可能更期待.css('background-color','')。这将从元素样式中删除它,因此将最后一个应用到链中。但不确定为什么透明不起作用。

更新

见这里: http://jsfiddle.net/

答案 2 :(得分:0)

试试这个:

$.each(theArray,function(intIndex,objValue){
    $("#" + objValue.id).parent().css('background-color','red').delay(1000).queue(function() {
        $(this).css('background-color','');
        $(this).dequeue();
    });
});

答案 3 :(得分:0)

.delay()仅适用于动画效果队列或自定义队列,没有jQuery UI的动画不适用于颜色......

相反,您可以使用setTimeout()来电:

$.each(theArray,function(intIndex,objValue){
    var myObj = $("#" + objValue.id).parent().css('background-color','red');
    setTimeout(function() {
        myObj.css('background-color','transparent');
    },1000);
});
相关问题