代码之家  ›  专栏  ›  技术社区  ›  eozzy

清除模糊文本区域(jQuery)

  •  0
  • eozzy  · 技术社区  · 15 年前
    // 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'));
        }
    });
    

    当我在文本区域外单击时,尽管文本区域中存在内容,但仍然可以看到背景图像。

    谢谢你的帮助!

    2 回复  |  直到 15 年前
        1
  •  2
  •   Community CDub    8 年前

    增加什么 Matt 说。 $this $(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'));
      }
    });
    
        2
  •  3
  •   Matt    15 年前