给jQuery增加join方法

正在做一个在线书签管理的站点,有需要这样一个功能,把匹配节点的id或值拼合起来以用来传递参数

查了一下,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(","));
});
原文地址:https://www.cnblogs.com/yashen/p/1887633.html