Home >Web Front-end >JS Tutorial >Add implementation code of join method to jQuery_jquery

Add implementation code of join method to jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 18:15:041346browse

After checking, jquery has no direct method,

, but there is a map method, which is very simple to define a method to implement this function

Copy the code The code is as follows:

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

When using
Copy the code The code is as follows:

$("#getid").click(function(){
alert($("input").joinattr(",","id"));
});
$("#getvalue").click(function(){
alert($("input").joinvalue(","));
});
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn