jQuery: textarea default value disppear on click

前端 未结 8 1176
Happy的楠姐
Happy的楠姐 2021-02-04 16:40

I want a textarea with some default text. When the user clicks in the textarea the default text should be deleted. How can I make value of a textarea disappear on click?

<
8条回答
  •  太阳男子
    2021-02-04 17:35

    I use this as its a bit more generic - it will clear out the element's value on focus, but return the element's value to the default value if empty.

    $("#textarea")
      .focus(function() {
            if (this.value === this.defaultValue) {
                this.value = '';
            }
      })
      .blur(function() {
            if (this.value === '') {
                this.value = this.defaultValue;
            }
    });
    

提交回复
热议问题