开发者

Clearing Textarea on Blur (jQuery)

开发者 https://www.devze.com 2023-01-04 17:25 出处:网络
// Clearing Textarea $(\'textarea\').focus(function() { var $this = $(this); $.data(this, \'img\', $this.css(\'background-image\'));
// Clearing Textarea
$('textarea').focus(function() {
   var $this = $(this);
   $.data(this, 'img', $this.css('background-image'));
   $this.css('background-image', 'none');
});
$('textarea').blur(function() {
    if($.trim($('textarea').val()).length){
         $this.css('background-image', 'none');
    } else {
        $(this).css('background-image', $.data(this, 'img'));
    }
});

When I click out of the textarea, and although there is content present in it, I still see the background image.

Thanks开发者_如何转开发 for your help!


In your blur function, you have $this, but it is never defined. You only defined it in the scope of the focus() function.


Adding to what Matt said. $this wasn't defined. What you needed to do was $(this):

$('textarea').blur(function() {
  if($.trim($('textarea').val()).length){
    // Added () around $this
    $(this).css('background-image', 'none');
  } else {
    $(this).css('background-image', $.data(this, 'img'));
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消