Javascript 如何使此代码简短&;最优的

Javascript 如何使此代码简短&;最优的,javascript,jquery,Javascript,Jquery,像这样: $(":input").blur(function(e){ $(this).css("background-color","white"); }); $(":input").focus(function(e){ $(this).css("background-color","#FDFCDC"); }); 或者,更好的是: $("#txtID, #txtPasswor

像这样:

        $(":input").blur(function(e){
            $(this).css("background-color","white");
        });
        $(":input").focus(function(e){
            $(this).css("background-color","#FDFCDC");
        });
或者,更好的是:

    $("#txtID, #txtPassword").focus(function(e){
        $(this).css("background-color","#FDFCDC");
    }).blur(function(e){
        $(this).css("background-color","white");
    });

#txtID,#txtPassword{
背景色:白色;
}
#txtID:active,#txtPassword:active{
背景色:#FDFCDC;
}
像这样:

        $(":input").blur(function(e){
            $(this).css("background-color","white");
        });
        $(":input").focus(function(e){
            $(this).css("background-color","#FDFCDC");
        });
或者,更好的是:

    $("#txtID, #txtPassword").focus(function(e){
        $(this).css("background-color","#FDFCDC");
    }).blur(function(e){
        $(this).css("background-color","white");
    });

#txtID,#txtPassword{
背景色:白色;
}
#txtID:active,#txtPassword:active{
背景色:#FDFCDC;
}

第二个更好,因为它不需要Javascript。另外,它可能会更快。但它是否与第一种方法在相同的浏览器中受支持?如果我错了,请纠正我,但我相信第二种解决方案在IE上不起作用(至少在某些版本上)。@levik:我相信你正在考虑
:hover
。谢谢分享你的想法。第二个可以在每个浏览器上正常工作(FF、IE678、GC)。第二个更好,因为它不需要Javascript。另外,它可能会更快。但它是否与第一种方法在相同的浏览器中受支持?如果我错了,请纠正我,但我相信第二种解决方案在IE上不起作用(至少在某些版本上)。@levik:我相信你正在考虑
:hover
。谢谢分享你的想法。第二个在每个浏览器(FF、IE678、GC)上都能正常工作。我建议您不要过早优化=/我建议您不要过早优化=/