juery文本框获得焦点和失去焦点是,对文本内容的处理

使用场合:在文本框中输入提示的默认值;还有在编辑的时候,改变后显示改变的值,没有有改变择显示以前的值, 也可以在加验证的时候进行封装.
css样式:
View Code
 1  <style type="text/css">
 2          body
 3          {
 4              font:  normal 12px/17px Arial;
 5          }
 6          div
 7          {
 8              padding:  2px;
 9              width:  240px;
10          }
11          input, textarea
12          {
13              width:  12em;
14              border:  1px solid #FFDEAD;
15          }
16          .focus
17          {
18              border:  1px solid #f00;
19              background:  #F5FFFA;
20          }
21      </style>
22 
23   

jquery代码:

View Code
 1  $(function() {
 2         $(": input").bind("focus", function() {
 3              $(this).addClass("focus");
 4                  if ($(this).val() == this.defaultValue) {
 5                     $(this).val("");
 6                  }
 7              });
 8         $(":input").bind("blur", function() {
 9              $(this).removeClass("focus");
10                  if ($(this).val() == '') {
11                      $(this).val(this.defaultValue);
12                  }
13             });
14        });

 

你可能感兴趣的:(文本框)