2017-03-08 113 views
0

我需要从阵列中创建链接的帮助。我需要的阵列使每个阵列项目单独链接

在这里,创建个性化链接,每一个项目的代码:

caption: function(instance, item) { 
    var caption, link, collectTags, tags; 
    caption = $(this).data('caption'); 
    link = '<a href="' + item.src + '">Download image</a>'; 
    collectTags = $(this).parent().attr("class").split(' '); 
    tags = $.each(function() { 
     '<a href="' + collectTags + '">' + collectTags + '</a>' 
    }); 
    return (caption ? caption + '<br />' : '') + link + '<br/>' + tags; 
} 
+0

那么,你的代码有什么问题? –

+0

你面临什么问题?你可以创建一个示例链接,以便我们可以看到你的问题,并试图纠正它? –

回答

1

你的代码可能是这样的,你叫$。每个不传递一个数组。

caption : function(instance, item) { 
    var caption, link, collectTags, tags; 

    caption = $(this).data('caption'); 
    link = '<a href="' + item.src + '">Download image</a>'; 
    collectTags = $(this).parent().attr("class").split(' '); 
    tags = $.map(collectTags,function(it){ return '<a href="' + it + '">'+ it +'</a>';}); 

    return (caption ? caption + '<br />' : '') + link + '<br/>' + tags; 

} 
+0

这列出了数组,但不为每个数组创建链接。 – Capwiz

+0

我已经更新了我的答案,这段代码对你来说工作得很好。你必须使用'map'而不是''每个' –

+0

令人惊叹的,谢谢! – Capwiz