Clear input on focus with jQuery and return on blur

后端 未结 12 2345
耶瑟儿~
耶瑟儿~ 2021-02-09 17:50

This almost works. However, when leaving the field \"defaulttext\" appears rather than the original text value. Not sure how to most efficiently echo the variable inside default

12条回答
  •  故里飘歌
    2021-02-09 18:17

       $('.myclass').each(function() {
        var default_value = this.value;
        $(this).css('color', '#666'); // this could be in the style sheet instead
        $(this).focus(function() {
            if(this.value == default_value) {
                this.value = '';
                $(this).css('color', '#333');
            }
        });
        $(this).blur(function() {
            if(this.value == '') {
                $(this).css('color', '#666');
                this.value = default_value;
            }
        });
    });
    
    

提交回复
热议问题