jQuery: textarea default value disppear on click

前端 未结 8 1180
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:37

    And if someone wants to do this trick on a ajax loaded textareas you can achieve this with the .live() event ;)

    $('#textarea').live('focusin',function () {
            if (this.value === 'leDefault ...') {
                this.value = '';
            }
        });
        $('.larger').live('focusout',function () {
            if (this.value === '') {
                this.value = 'leDefault';
            }
        });
    
    0 讨论(0)
  • 2021-02-04 17:38
    $('#textarea').click(function(){
         if($(this).val() == "This should be removed.."){
              $(this).val() = "";
         }
    });
    

    //edit

    var defaultTextAreaValue = "This should be removed..";
    $('#textarea').focus(function(){
         if($(this).val() == defaultTextAreaValue){
             $(this).val("");
         }
    });
    $('#textarea').blur(function(){
          if($(this).val() == "" || $(this).val() == defaultTextAreaValue){
              $(this).val(defaultTextAreaValue);
          }
    });
    
    0 讨论(0)
提交回复
热议问题