Javascript 基于数组函数结果的If..else语句

Javascript 基于数组函数结果的If..else语句,javascript,jquery,url,if-statement,fancybox,Javascript,Jquery,Url,If Statement,Fancybox,我希望根据数组函数的结果提供不同的URL。 基本上,如果collectTags等于“church”或“concert”,它将链接到A.com,否则它将链接到B.com 以下是我目前拥有的代码: caption : function( instance, item ) { var caption, link, collectTags, tags; caption = $(this).data('caption'); link = '<a href="' + i

我希望根据数组函数的结果提供不同的URL。 基本上,如果collectTags等于“church”或“concert”,它将链接到A.com,否则它将链接到B.com

以下是我目前拥有的代码:

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){ if(collectTags === "church"){ return '<a href="A.com' + it + '">'+ it +'</a>'} else{return '<a href="B.com' + it + '">'+ it +'</a>'};});

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

}
标题:函数(实例,项){
变量标题、链接、集合标记、标记;
caption=$(this.data('caption');
链接='';
collectTags=$(this.parent().attr(“类”).split(“”);
tags=$.map(collectTags,函数(it){if(collectTags===“church”){return'}else{return'};});
return(caption?caption+'
':'')+link+'
'+tags.slice(1); }
我不确定自己是否能做好,但我会试试看。您的问题是您再次访问collectTags。这是一个数组,而不是字符串,因此将其与字符串进行比较总是错误的。永远不要使用字符串压缩,这会使你的代码更难阅读和混乱

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

        function format(tpl, binding) {
            if (typeof binding != 'function') return format(tpl, function (_, name) {
                return binding[name];
            });
            return tpl.replace(/\$(\w+)/g, binding);
        }

        caption = $(this).data('caption');
        link = format('<a href="$src">Download image</a>', item);
        collectTags = $(this).parent().attr("class").split(' ');
        function createTag(it) {
            return format("<a href='$site/$it'>$it</a>", {
                site: (it == 'church' || it == 'concert') ? 'A.com' : 'B.com',
                it: it
            });

        }

        tags = $.map(collectTags, createTag);
        return [].concat(caption ? [caption, link] : link).concat(tags).join('<br/>');
    }
}
{
标题:函数(实例、项){
变量标题、链接、集合标记、标记;
函数格式(tpl,绑定){
if(typeof binding!=“function”)返回格式(tpl,function(uz,name){
返回绑定[名称];
});
返回tpl。替换(/\$(\w+)/g,绑定);
}
caption=$(this.data('caption');
链接=格式(“”,项目);
collectTags=$(this.parent().attr(“类”).split(“”);
函数createTag(it){
返回格式(“”{
网站:(it='church'| | it='concert')?'A.com':'B.com',
它:它
});
}
标记=$.map(collectTags,createTag);
return[].concat(标题?[caption,link]:link).concat(标记).join(“
”); } }
我不确定自己是否能做好,但我会试试看。您的问题是您再次访问collectTags。这是一个数组,而不是字符串,因此将其与字符串进行比较总是错误的。永远不要使用字符串压缩,这会使你的代码更难阅读和混乱

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

        function format(tpl, binding) {
            if (typeof binding != 'function') return format(tpl, function (_, name) {
                return binding[name];
            });
            return tpl.replace(/\$(\w+)/g, binding);
        }

        caption = $(this).data('caption');
        link = format('<a href="$src">Download image</a>', item);
        collectTags = $(this).parent().attr("class").split(' ');
        function createTag(it) {
            return format("<a href='$site/$it'>$it</a>", {
                site: (it == 'church' || it == 'concert') ? 'A.com' : 'B.com',
                it: it
            });

        }

        tags = $.map(collectTags, createTag);
        return [].concat(caption ? [caption, link] : link).concat(tags).join('<br/>');
    }
}
{
标题:函数(实例、项){
变量标题、链接、集合标记、标记;
函数格式(tpl,绑定){
if(typeof binding!=“function”)返回格式(tpl,function(uz,name){
返回绑定[名称];
});
返回tpl。替换(/\$(\w+)/g,绑定);
}
caption=$(this.data('caption');
链接=格式(“”,项目);
collectTags=$(this.parent().attr(“类”).split(“”);
函数createTag(it){
返回格式(“”{
网站:(it='church'| | it='concert')?'A.com':'B.com',
它:它
});
}
标记=$.map(collectTags,createTag);
return[].concat(标题?[caption,link]:link).concat(标记).join(“
”); } }
代码出了什么问题?你还在吗?代码出了什么问题?你还在吗?这似乎起了作用。我想我可以在此基础上进一步阐述,这似乎已经达到了目的。我想我可以在此基础上进行扩展