2013-05-07 55 views
1

我此腳本的jQuery:jQuery的焦點,當無焦點的方式改回原來的CSS

<script> 
$('#searchbar').focus(function(){ 
    $(this).parent().css('background-color', 'red'); 
}); 
</script> 

當你專注的孩子在父母的父母背景變爲紅色。但是當你離開焦點時,它仍然是紅色的。如何讓它變成另一種顏色,讓我們說藍色,當你把孩子放鬆時?

孩子是一種形式。

回答

5

添加事件的內容功能

$('#searchbar').focus(function(){ 
    $(this).parent().css('background-color', 'red'); 
}).blur(function() { 
    //do what you need 
    $(this).parent().css('background-color', 'blue'); 
}); 
+0

非常感謝!這是一個快速的答案。祝你今天愉快! – user2336174 2013-05-07 16:09:44

+0

ITYM'模糊' - 'focusout'事件對應於'focusin'事件。 – Alnitak 2013-05-07 16:10:44

+0

@Alnitak感謝您的信息 - 更新了答案。 – tymeJV 2013-05-07 16:13:15

1

您可以使用blur()

$('#searchbar').blur(function() { 
    $(this).parent().css('background-color', 'black'); 
});