How to target all input text and password value?

前端 未结 2 472
悲哀的现实
悲哀的现实 2021-01-25 18:33

I\'m trying to figure out how to target every input(text and password) field on all forms on the page document to remove the default value when on focus with this single script:

2条回答
  •  礼貌的吻别
    2021-01-25 18:41

    That's because val only returns the value of the first selected element, instead of storing the values, you can use defaultValue property of the HTMLInputElement object.

    $('input[type=text], input[type=password]').focus(function() {
       if (this.value === this.defaultValue) $(this).val("");
    }).blur(function(){
       if (this.value.length === 0) this.value = this.defaultValue;
    });
    

    http://jsfiddle.net/MscgZ/

提交回复
热议问题