2013-02-06 29 views
1

我目前正在使用這個腳本,並將很喜歡延長它來改變填充幫助的字段的顏色嗎?謝謝javascript輸入顏色後輸入

<script> 
    $(document).ready(function(){ 
    $("input").focus(function(){ 
    $(this).css("background-color","#cccccc"); 
    }); 

    $("input").blur(function(){ 
    $(this).css("background-color","#ffffff"); 
    }); 
    }); 
</script> 
+0

您需要定義「填充」的含義。這聽起來像你只是想在你的onblur處理程序中有一個不同的條件,如果該字段仍然是空的,那麼它就是一種顏色,而另一種情況是「填充的」,這取決於你的「填充」標準。 –

回答

1

剛檢查模糊區域是否填滿:

$("input").on('blur', function() { 
    if ($(this).val()) { 
     $(this).css("background-color","#0f0"); 
    } 
    else { 
     $(this).css("background-color","#fff"); 
    } 
}); 
+0

感謝顏色代碼將跨平臺相同 – user1940979

1

儘管這不是「爲您編寫腳本」網站,但您已經提出了一個簡單的問題,可以給出一個簡單的答案。看看你能不能在下一次弄明白爲什麼這個作品,並且,你問一個問題之前,試圖拼湊從這個和其他代碼位的答案你已經毫無疑問,網絡上發現:

$("input").change(function(){ // this just checks if there's anything in the field 
    if($(this).val().length > 0)) { 
     $(this).css("background-color", "green"); // input has been filled 
    } else { 
     $(this).css("background-color", "red"); // input has not been filled 
    } 
}); 
+0

自動填充功能如何? – Petah

+0

這是怎麼回事? '$ .change()'檢測輸入值的任何變化,無論這種變化來自何處。 –

+0

http://stackoverflow.com/questions/3066406/jquery-what-listener-do-i-use-to-check-for-browser-auto-filling-the-password-in – Petah