如果在jquery,输入字段中为空,则删除/添加onfocus/onblur上的值

Kar*_*rem 8 jquery

我知道这方面存在很多问题,但我不知道该搜索什么.

我有一个电子邮件字段,我希望它有"在这里写你的电子邮件",当你点击/做onfocus它应该消失.虽然如果你没有写东西并且继续写它,它应该再次出现.

She*_*hef 10

1.标记

<input id="email" name="email" type="text" value="Write your email here" />
Run Code Online (Sandbox Code Playgroud)

2. jQuery

$('#email')
  .on('focus', function(){
      var $this = $(this);
      if($this.val() == 'Write your email here'){
          $this.val('');
      }
  })
  .on('blur', function(){
      var $this = $(this);
      if($this.val() == ''){
          $this.val('Write your email here');
      }
  });?
Run Code Online (Sandbox Code Playgroud)

3.演示

jQuery输入占位符