检测焦点上的输入值长度

detect input value length on focus

当用户开始在 input 中输入时,我希望输入能做一些事情,所以我尝试这样写:

$('input').focus(function(){
  if ( $('input').val().length > 0 ) {
    $('input').css('color', 'red');
  }
  else {
    $('input').css('color', 'blue');
  }
});

只是为了了解如何检测 input 何时不为空
我做错了什么?

试试这个Demo here

$('input').keyup(function(){
  if ( $(this).val().length > 0 ) {
    $(this).css('color', 'blue');
  }
  else {
    $(this).css('color', 'red');
  }
});