JQuery文本框 获得焦点 背景颜色改变

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="js/jquery-1.7.js" type="text/javascript">
</script> <script type="text/javascript">
$(function () {
$("text").each().mouseover(function () { $(this).css("backgrounp", "red"); });
});
</script>
</head>
<body>
<div><input type="text" value="sdasjd"/></div>
<div><input type="text" value="sda"/></div>
<div><input type="text" value="sdasjl"/></div>
</body>
</html>
每当任意一个文本框获得焦点时 或者光标在文本框内时 将文本框的背景颜色改成红色,离开后恢复成 白色,用JQuery实现。上面的代码怎么样改才能实现这个功能?谢谢

1、先使用jQuery选择器找到所有的文本框

2、为文本框注册获得焦点事件,即focus事件

3、在焦点事件的事件处理函数中对当前得到焦点的文本框设置背景色

4、注册失去焦点事件,即blur事件

5、在失去焦点的事件处理函数中对当前触发事件的文本框改变背景颜色

<script type="text/javascript">
    $(function(){
        //找到文本框,并注册得到焦点事件
        $("input:text").focus(function(){
            //让当前得到焦点的文本框改变其背景色
            $(this).css("background","pink");
        });
        //找到文本框,并注册失去焦点事件
        $("input:text").blur(function(){
            //让当前失去焦点的文本框背景色变为白色
            $(this).css("background","white");
        });
    });
</script>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-11-16
$(":text").focus(function () { $(this).css("background", "red"); }).blur(function () { $(this).css("background", "#fff"); })本回答被提问者和网友采纳