我希望为2个jQuery对象运行相同的函数:$('input[type="text"]')
和$('textarea[type=text]')
.如何在下面的代码中将这两者结合起来?(目前,仅包括输入).
$('input[type="text"]').focus(function() {
if (this.value == this.defaultValue){
this.value = '';
}
if(this.value != this.defaultValue){
this.select();
}
});
$('input[type="text"]').blur(function() {
if ($.trim(this.value == '')){
this.value = (this.defaultValue ? this.defaultValue : '');
}
});
Run Code Online (Sandbox Code Playgroud)
谢谢!
试试这个:
$('textarea[type="text"], input[type="text"]').focus(...).blur(...);
Run Code Online (Sandbox Code Playgroud)
同样你也可以使用jQuery的add
功能:
$('textarea[type="text"]').add('input[type="text"]').focus(...).blur(...);
Run Code Online (Sandbox Code Playgroud)