jQuery效果之文本框失去焦点

来源:互联网 发布:可爱的毛衣淘宝店铺 编辑:程序博客网 时间:2024/05/01 01:18
<!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=gb2312" />
<title>无标题文档</title>
<link rel="stylesheet" type="text/css" href="style/hdw.css"/>
<script type="text/javascript" src="js/jquery-1.6.4.min.js"></script>
<script type="text/javascript">


    $(document).ready(function(){
        
        
        $(":input").focus(function(){
            
            $(this).addClass("focusa");
            
            if ($(this).val()==this.defaultValue){
                
                $(this).val("");
                
                }


            }).blur(function(){
    
            $(this).removeClass("focusa");    
            
            
            if ($(this).val()==""){
                
                $(this).val(this.defaultValue);
                
                
                }

                
                });

        
        });



</script>
</head>

<body>
<form action="" method="post">
  <fieldset>
  <legend>个人信息</legend>
<div>
    <label for="username">姓名:</label>
    <input type="text" id="username" value="小王" />
  </div>
  <div>
    <label for="pass">密码:</label>
    <input type="password" id="pass" value="123" />
  </div>
  <div>
    <label for="hdw">内容:</label>
    <textarea id="hdw">内容</textarea>
  </div>
</fieldset>
</form>
</body>
</html>


CSS

/* CSS Document */
* { padding:0; margin:0;}

body { font-size:12px; font-family:"宋体";}

form { width:500px; position:absolute; top:50%; left:50%; margin:-70px 0 0 -250px;}

fieldset { border:2px solid green; padding:10px;}

legend { font-size:13px; font-weight:bold; padding:10px; color:green;}

div { padding:5px 0;}

label { color:green;}

input { height:25px; border:1px solid #C9C9C9; width:200px; font-size:12px; font-family:"宋体"; padding:0 5px; line-height:25px;}

textarea { width:200px; height:100px; border:1px solid green; font-size:12px; font-family:"宋体"; padding:5px; vertical-align:middle;}

.focusa { border:1px solid green; background:#FFF0FF;}


0 0
原创粉丝点击