2015-08-08 44 views
0

我的樣品訂單事件我如何在jQuery中

$('#cool').hover(function() { 
 
    $(this).css({ 
 
    "background-color": "red" 
 
    }); 
 
    $('#fool').css({ 
 
    "background-color": "green" 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
 
<html> 
 

 
<body> 
 
    <div id=fool> 
 
    <div id=cool> 
 
    </div> 
 
    </div> 
 
</body> 
 
</html>

代碼下面給出

的問題是我怎麼裝傻事件後執行涼爽事件。在給定的代碼中,兩者都在同一時間運行。 我如何訂購活動。 如何在完成fool.css()後運行cool.css()

+0

這是你在找什麼? https://jsfiddle.net/cf59f6be/試圖找出想要的結果 –

+0

「酷事件」和「傻瓜事件」是什麼意思? – chayasan

+1

'.css'不是一個事件..它沒有持續時間。它立即應用。 – putvande

回答

0

調用.css()的結果將同時應用:下一次重新繪製頁面。爲了能夠以任何特定順序添加它們,您將不得不在.css()調用之間強制重畫。

強制重繪的最簡單方法是讀取元素的heightwidth

$('#cool').hover(function() { 
    $('#fool').css({ 
     "background-color": "green" 
    }); 

    $(this).height(); // Force repaint 

    $(this).css({ 
     "background-color": "red" 
    }); 
}); 
0
$('#cool').hover(function(){ 
    var dis=$(this); 
    $(this).parent().animate({'background-color':'green'},500,function(){dis.animate({'background-color':'red'},500);}); 
}); 
+0

請考慮在答案中增加更多細節。 –