jQuery增加链式方法

1
2
3
4
5
6
7
8
9
10
11
(function(){
jQuery.fn.join = function(sep,mapvalue){
return $.map(this,mapvalue).join(sep);
};
jQuery.fn.joinattr = function(sep,attr){
return this.join(sep,function(item){return $(item).attr(attr);});
};
jQuery.fn.joinvalue = function(sep){
return this.join(sep,function(item){return $(item).val();});
};
})(jQuery)

使用的时候

1
2
3
4
5
6
$("#getid").click(function(){
alert($("input").joinattr(",","id"));
});
$("#getvalue").click(function(){
alert($("input").joinvalue(","));
});