Get default value of an input using jQuery

前端 未结 6 1046
梦谈多话
梦谈多话 2021-01-04 00:53
$(\".box_yazi2\").each(function () {
    var default_value = this.value;
    $(this).css(\'color\', \'#555\'); // this could be in the style sheet instead
    $(this         


        
6条回答
  •  我在风中等你
    2021-01-04 01:41

    You should use prop instead of so many functions to be honest, use 'delegate' instead of 'on' for late static binding.

    $('.box_yazi2').each(function() {
    
    $(this).on('focus', function(){
    
       if($(this).val() == $(this).prop('defaultValue')){
    
          $(this).val('');
          $(this).css('color', '#000');
        }
    
    });
    
    $(this).on('blur', function(){
    
       if($(this).val() == ''){
    
          $(this).val($(this).prop('defaultValue'));
          $(this).css('color', '#000');
       }
    
    });
    
    });
    

提交回复
热议问题