多个jQuery对象的相同功能

Esp*_*noy 2 javascript jquery

我希望为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)

谢谢!

Xav*_*avi 9

试试这个:

$('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)