<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>表单操作</title>
<script language="javascript" src="scripts/jquery-1.4.1.js"></script>
<style type="text/css">
.focus:{
border:1px solid #foo;
background:#fcc;
}
input:focus,textarea:{
border:1px solid #foo;
background:#fcc;
}
</style>
<script language="javascript" type="text/javascript">
function test(){
$(document).ready(function(){
   $(":input").focus(function(){<!--获得焦点调用的函数-->
$(this).addClass("focus");
}).blur(function(){<!--失去焦点调用的函数 -->
$(this).removeClass("focus");
});
   
   });
}
</script>
</head><body><form method="post" id="regForm">
<fieldset>
<legend>个人基本信息</legend>
<div>
<label for="username">名称:</label>
    <input id="username" type="text" onblur="test()" onfocus="test()"/>
</div><div>
<label for="pass">密码:</label>
    <input id="pass" type="password" onblur="test()" onfocus="test()"/>
</div><div>
<label for="msg">详细信息:</label>
    <textarea id="pass"></textarea>
</div>
</fieldset>
</form>
</body>
</html>
哪位大哥大姐帮我看看这个test函数好像不起作用啊?

解决方案 »

  1.   


    把test函数删除了,直接把方法体里面的内容放到外面。。
      

  2.   


    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>表单操作</title>
    <script language="javascript" src="scripts/jquery-1.4.1.js"></script>
    <style type="text/css">
        .focus:{
            border:1px solid #foo;
            background:#fcc;
            }
        input:focus,textarea:{
            border:1px solid #foo;
            background:#fcc;
            }
    </style>
    <script language="javascript" type="text/javascript">
    /*
    function test(){
        $(document).ready(function(){
                   $(":input").focus(function(){<!--获得焦点调用的函数-->
                            $(this).addClass("focus");
                            }).blur(function(){<!--失去焦点调用的函数 -->
                            $(this).removeClass("focus");
                            });
                   
                   });
    }
    */
    //test(); $(document).ready(function(){
                   $(":input").focus(function(){<!--获得焦点调用的函数-->
                            $(this).addClass("focus");
                            }).blur(function(){<!--失去焦点调用的函数 -->
                            $(this).removeClass("focus");
                            });
                   
                   });
    </script>
    </head><body><form method="post" id="regForm">
    <fieldset>
    <legend>个人基本信息</legend>
    <div>
        <label for="username">名称:</label>
        <input id="username" type="text" onblur="test()" onfocus="test()"/>
    </div><div>
        <label for="pass">密码:</label>
        <input id="pass" type="password" onblur="test()" onfocus="test()"/>
    </div><div>
        <label for="msg">详细信息:</label>
        <textarea id="pass"></textarea>
    </div>
    </fieldset>
    </form>
    </body>
    </html>
      

  3.   

    一步一步测试,首先在$(document).ready 前面输入一个alert(),看运行到那里没有,如果没有运行到那里,也许就是的test函数,里面有js错误,加载的时候就报错了。。不能执行。。
    然后再一步一步验证下去,可以用IE7或者IE8自带的工具进行DEBUG调试,
    按F12呼出。
      

  4.   

    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>表单操作</title>
    <script language="javascript" src="scripts/jquery-1.4.1.js"></script>
    <style type="text/css">
    .focus:{
    border:1px solid #foo;
    background:#fcc;
    }
    input:focus,textarea:{
    border:1px solid #foo;
    background:#fcc;
    }
    </style>
    <script language="javascript" type="text/javascript">
    $(document).ready(function(){
       $(":input").focus(function(){<!--获得焦点调用的函数-->
    $(this).addClass("focus");
    }).blur(function(){<!--失去焦点调用的函数 -->
    $(this).removeClass("focus");
    });
       
       });</script>
    </head><body><form method="post" id="regForm">
    <fieldset>
    <legend>个人基本信息</legend>
    <div>
    <label for="username">名称:</label>
        <input id="username" type="text"/>
    </div><div>
    <label for="pass">密码:</label>
        <input id="pass" type="password"/>
    </div><div>
    <label for="msg">详细信息:</label>
        <textarea id="pass"></textarea>
    </div>
    </fieldset>
    </form>
    </body>
    </html>
    这样也不可以啊?一点效果也没有
      

  5.   

    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
    <title>表单操作</title>
    <script language="javascript" src="scripts/jquery-1.4.1.js"></script>
    <style type="text/css">
        .focus:{
            border:1px solid #ff0000;
            background:#ff0000;
            }
        input:focus,textarea:{
            border:1px solid #ff0000;
            background:#ff0000;
            }
    </style>
    <script language="javascript" type="text/javascript">
    function test(){
        $(document).ready(function(){
                   $(":input").focus(function(){<!--获得焦点调用的函数-->
                            $(this).addClass("focus");
                            });
                   
                   });
       $(":input").blur(function(){<!--失去焦点调用的函数 -->
                            $(this).removeClass("focus");
                            });}
    </script>
    </head><body><form method="post" id="regForm">
    <fieldset>
    <legend>个人基本信息</legend>
    <div>
        <label for="username">名称:</label>
        <input id="username" type="text" onblur="test()" onfocus="test()"/>
    </div><div>
        <label for="pass">密码:</label>
        <input id="pass" type="password" onblur="test()" onfocus="test()"/>
    </div><div>
        <label for="msg">详细信息:</label>
        <textarea id="pass"></textarea>
    </div>
    </fieldset>
    </form>
    </body>
    </html>test函数是可以调用,但是你加的那个样式没有加上。。你自己仔细看下
      

  6.   


    <style type="text/css">
        .focus{
            border:1px solid #foo;
            background:#fcc;
            }
        input.focus,textarea{
            border:1px solid #foo;
            background:#fcc;
            }
    </style>