为jQuery增加join方法的实现代码


查了一下,jquery没有直接的方法,

不过有一个map方法,使用它很简单就可以定义一个实现该功能的方法
复制代码 代码如下:

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();});
};

使用的时候
复制代码 代码如下:

$("#getid").click(function(){
alert($("input").joinattr(",","id"));
});
$("#getvalue").click(function(){
alert($("input").joinvalue(","));
});

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3