jquery 设置占位符

<script type="text/javascript">
    $(document).ready(function(){
       $('.inputfield').each(function(){
           $(this).val('');
           add_placeholder($(this));
       })

       $('.inputfield').focus(function(){
           remove_placeholder($(this));
       }).blur(function(){
           add_placeholder($(this));
       }).blur();
    });

   function add_placeholder(field)
   {
       var placeholder = field.attr('name');
       if(field.val() == '')
       {
            field.val(placeholder);
            field.addClass('placeholder');
       }
   }

   function remove_placeholder(field)
   {
       var placeholder = field.attr('name');
       if(field.val() == placeholder)
       {
            field.val('');
            field.removeClass('placeholder');
       }
   }

</script>

posted on 2014-09-23 15:06  Lishenyin  阅读(721)  评论(0编辑  收藏  举报

导航