Javascript text input fields that display instruction (placeholder) text

后端 未结 7 2071
抹茶落季
抹茶落季 2021-02-04 13:17

What\'s the best way to have a text input field that displays instructions of what to enter in that field in gray. When you focus on that input field, the gray instruction text

相关标签:
7条回答
  • 2021-02-04 13:46

    html

    <input type="text" id="user" class="textbox default" value="Enter login name">
    <input type="password" id="pass" class="textbox">
    

    jQuery

    $(function(){
      $('.textbox').focus(function(){
        if (this.value == this.defaultValue) {
            this.value = '';
            $(this).removeClass('default');
        };
      }).blur(function(){
        if (this.value == '') {
            this.value = this.defaultValue;
            $(this).addClass('default');
        };
      });​
    });
    

    demo

    0 讨论(0)
提交回复
热议问题