jQuery/javascript - Input fields (user,pass) just like twitter's login?

后端 未结 6 693
萌比男神i
萌比男神i 2021-01-21 13:14

How can I achieve the same effect like twitter\'s log-in form (the top on the right) - when you click on the username/pass the value \"username\" stays there until you enter tex

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-21 14:05

    HTML:

    
    

    jQuery:

    $('input.username')
    .focus(function(){
      var $this = $(this);
      if ($this.val() == $this.attr('title')) {
        $this.val('').removeClass('dimmed');
      }
    })
    .blur(function(){
      var $this = $(this);
      if ($this.val().length == 0){
        $this.val($this.attr('title')).addClass('dimmed');
      }
    });
    

    CSS:

    input.username {
      color: #000;
    }
    input.username.dimmed {
      color: #888;
    }
    

提交回复
热议问题