输入后javascript更改输入颜色

时间:2013-02-06 00:32:21

标签: javascript jquery javascript-events

我目前正在使用这个脚本,并希望扩展它以更改已填充的字段的颜色帮助吗?感谢

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

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

2 个答案:

答案 0 :(得分: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
    }
});

答案 1 :(得分:1)

只需检查字段是否填充模糊:

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