2013-02-06 27 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