您的位置:首页 > Web前端 > JQuery

jQuery join详细说明与实例代码

2013-09-05 20:23 573 查看
jquery.map( array, callback(elementofarray, indexinarray) )

看一实例
<!doctype html>
<html>
<head>
<style>
div { color:blue; }
p { color:green; margin:0; }
span { color:red; }
</style>
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
</head>
<body>
<div></div>
<p></p>
<span></span>

<script>
var arr = [ "a", "b", "c", "d", "e" ];
$("div").text(arr.join(", "));
arr = jquery.map(arr, function(n, i){
return (n.touppercase() + i);
});
$("p").text(arr.join(", "));
arr = jquery.map(arr, function (a) { return a + a; });
$("span").text(arr.join(", "));
</script>
</body>
</html>
结果
a, b, c, d, e
a0, b1, c2, d3, e4
a0a0, b1b1, c2c2, d3d3, e4e4

方法二

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(","));
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: