如何使用javascript打开新标签中的数组链接?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用javascript打开新标签中的数组链接?相关的知识,希望对你有一定的参考价值。

我在一个javascript数组中获取RSS feed并在html列表中显示。但我希望当用户点击链接时,必须在新标签页中打开它。

// HTML

<ol class="list">
</ol>

// Java脚本

<script>    


$(function() {
    getRssFeed("https://amirtariq69.blogspot.com/feeds/posts/default?alt=rss", mapFeed);

});


function getRssFeed(url, callback) {
    return feednami.loadGoogleFormat(encodeURI(url), callback);
}

function mapFeed(result) {
    if (result.error) {
      console.log(result.error)
  } else {
        createCarouselList(result.feed.entries.slice(0, 5));
        createFeedList(result.feed.entries.slice(0, 10));
  }
}


function createCarouselList(elements) {
    var list = [];
    $(elements).each(function(index, element) {
        list.push("<li><h3><a href='"+ element.link +"'>"+ element.title +"</a></h3><p>"+ new Date(element.publishedDate).toLocaleDateString("pt-BR") +"</p><span class='carousel-footer'>"+ (index + 1) +" out of 5</span></li>");
    });

    $(".carousel").append(list);

}


// Edit this function please ...
function createFeedList(elements) {
    var list = [];
    $(elements).each(function(index, element) {
        list.push("<li><a href='"+ element.link +"'>"+ element.title + "&nbsp; ["+ new Date(element.publishedDate).toLocaleDateString("pt-BR") +"] "+"</a></li>");
    });
    $(".list").append(list);
    returnCarouselList();
}

    </script>

我提到编辑功能的任何人都可以将此列表设置为(在新选项卡中打开)列表。

答案

解决了

function createFeedList(elements) {
    var list = [];
    $(elements).each(function(index, element) {
        list.push("<li><a href='"+ element.link + "' target='_blank +'>"+ element.title + "&nbsp; ["+ new Date(element.publishedDate).toLocaleDateString("pt-BR") +"] "+"</a></li>");
    });
    $(".list").append(list);
    returnCarouselList();
}
``````````````````````

另一答案

添加到标签:

target="_blank"
另一答案

尝试添加目标属性target =“_ blank”>如下所示

<script>    


$(function() {
    getRssFeed("https://amirtariq69.blogspot.com/feeds/posts/default?alt=rss", mapFeed);

});


function getRssFeed(url, callback) {
    return feednami.loadGoogleFormat(encodeURI(url), callback);
}

function mapFeed(result) {
    if (result.error) {
      console.log(result.error)
  } else {
        createCarouselList(result.feed.entries.slice(0, 5));
        createFeedList(result.feed.entries.slice(0, 10));
  }
}


function createCarouselList(elements) {
    var list = [];
    $(elements).each(function(index, element) {
        list.push("<li><h3><a href='"+ element.link +"target="_blank">"+ element.title +"</a></h3><p>"+ new Date(element.publishedDate).toLocaleDateString("pt-BR") +"</p><span class='carousel-footer'>"+ (index + 1) +" out of 5</span></li>");
    });

    $(".carousel").append(list);

}


// Edit this function please ...
function createFeedList(elements) {
    var list = [];
    $(elements).each(function(index, element) {
        list.push("<li><a href='"+ element.link +" target="_blank">"+ element.title + "&nbsp; ["+ new Date(element.publishedDate).toLocaleDateString("pt-BR") +"] "+"</a></li>");
    });
    $(".list").append(list);
    returnCarouselList();
}

    </script>

以上是关于如何使用javascript打开新标签中的数组链接?的主要内容,如果未能解决你的问题,请参考以下文章

如何在javascript中捕获新标签事件中的打开链接?

如何让新标签页打开新标签页中的网页。

如何使用Selenium WebDriver打开新标签页并启动链接?

如何使iframe中的链接都在新窗口打开?

新标签页中的 HTML 按钮打开链接

如何使用HTML实现点击一个链接打开新窗口49