在新窗口中打开Bootstrap模式中的任何链接 [英] Make any links in a Bootstrap modal open in a new window
问题描述
我有一个页面,该页面将内容加载到Bootstrap(3.3.7)模态窗口中.这些模态中的大多数内容具有1个或多个链接.
I have a page which loads content into a Bootstrap (3.3.7) modal window. The content in the majority of these modals features 1 or more links.
我希望这些模式下用户单击的任何链接在新的浏览器选项卡中打开.
I want any link that the user clicks on, in these modals, to open up in a new browser tab.
我可以通过在每个锚点上放置target="_blank"
来使此工作正常进行,例如
I can get this working by putting a target="_blank"
on each anchor, e.g.
<a href="http://www.google.com/" target="_blank">Link</a>
但这似乎很乏味,因为每次我输出链接时都必须添加此标签.
But this seems tedious as every time I output a link I have to add this tag.
有没有一种方法可以告诉浏览器在新窗口/标签页中打开模态中找到的所有链接?
Is there a way to just tell the browser to open any links found in a modal in a new window/tab?
我已经搜索过了,但是大多数答案都涉及将链接内加载到模式中,这不是我在这里想要做的.
I've searched around but most of the answers refer to loading the link inside the modal, which is not what I want to do here.
我还希望模式保持不变,即在上一个选项卡中将其保持打开状态(不要关闭).
I also want the modal to remain un-touched, i.e. keep it open (don't close it), in the previous tab.
推荐答案
您可以通过针对元素的jQuery添加target属性:
You can add the target attribute via jQuery targeting the elements:
$('#modal-name a').attr('target', '_blank');
或者您可以使用 window.open():
$('#modal-name').on('click', 'a', function(event) {
event.preventDefault();
window.open($(this).attr('href'), '_blank');
});
这篇关于在新窗口中打开Bootstrap模式中的任何链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!