首页 > 编程 > JavaScript > 正文

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

2019-11-21 00:04:56
字体:
来源:转载
供稿:网友
查了一下,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(","));
});
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表