2017-08-16 56 views
0

我有以下代碼:如何在jQuery中使用模糊處理?

$(function() { 
    $('.type_choice_textarea').on('focus', function() { 
     $(this).css("height", "150px"); 
    }); 
    if ($('.type_choice_textarea').val().length == 0) { 
     $('.type_choice_textarea').on('blur', function() { 
      $(this).css("height", "30px"); 
     }); 
    } 
}); 

blur不起作用。如何在blur中使用if

+2

移動'了'blur'事件處理中if'條件中的條件。 ($(this).val()。length == 0){ \t \t $(this).css(「if(')')。高度「,」30px「); \t} }); ' – Satpal

+1

你的代碼對我來說工作得很好 –

+0

@CarstenLøvboAndersen,取決於你想要它做什麼:) – TricksfortheWeb

回答

1

只要寫你,如果模糊事件處理程序

$(function() { 
    let elem = '.type_choice_textarea'; 

    $(elem).on('focus', function() { 
     $(this).css("height", "150px"); 
    }); 

    $(elem).on('blur', function() { 
     if ($(this).val().length == 0) {    
     $(this).css("height", "30px"); 
     } 
    }); 
});