Javascript 有没有办法在新窗口中打开页面上的所有 <a href> 链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12235585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:28:10  来源:igfitidea点击:

Is there a way to open all <a href> links on a page in new windows?

javascripthtmlhyperlink

提问by Tina CG Hoehr

I have a bunch of <a href=".html">links and want to make them all open in new windows.

我有一堆<a href=".html">链接,想让它们都在新窗口中打开。

I know I can do a search and replace all to add target="_blank"to all my <a href="...">links.

我知道我可以进行搜索并全部替换以添加target="_blank"到我的所有<a href="...">链接中。

However, is there a quick way, such as setting a CSS style, or adding some JavaScript code, to accomplish the same thing?

但是,有没有一种快速的方法,比如设置一个 CSS 样式,或者添加一些 JavaScript 代码,来完成同样的事情?

回答by Lekensteyn

If you have a page consisting of only links, consider <base target="_blank">. This opens every link in a new window (but also includes the targets of forms, unless overridden with <form target="_self">.

如果您的页面仅包含链接,请考虑<base target="_blank">. 这会在新窗口中打开每个链接(但也包括表单的目标,除非被<form target="_self">.

As others have shown, without modifying the HTML source, you can use Javascript to iterate through all <a>tags and add the targetattributeor add an event listener that sets the target attribute dynamically.

正如其他人所展示的,无需修改 HTML 源代码,您就可以使用 Javascript遍历所有<a>标签并添加target属性或添加动态设置目标属性事件侦听器

回答by Scott

If you have jQuery it's simple

如果你有 jQuery,那很简单

$("a").attr("target", "_blank");

Or regular Javascript

或者普通的 Javascript

var links = document.links;
for (var i = 0; i < links.length; i++) {
     links[i].target = "_blank";
}

As per @Lekensteyn's suggestion, without Javascript (added for Completeness)

根据@Lekensteyn 的建议,没有 Javascript(为了完整性而添加)

<base target="_blank">.

回答by Rob W

CSS: No.
JavaScript: Delegate a click event, which adds a target="_blank"attribute on click of a link.

CSS:否。
JavaScript:委托一个点击事件,它target="_blank"在点击链接时添加一个属性。

document.body.addEventListener(function(e) {
    if (e.target.nodeName.toUpperCase() === 'A' && e.target.href) {
        e.target.target = '_blank';
    }
}, true);

Note: If the <a>element contains other elements, you might want to traverse the tree to find out whether an anchor element is clicked:

注意:如果<a>元素包含其他元素,您可能需要遍历树以找出是否单击了锚元素:

document.body.addEventListener(function(e) {
    var target = e.target;
    do {
        if (target.nodeName.toUpperCase() === 'A' && target.href) {
            target.target = '_blank';
            break;
        }
    } while (target = target.parentElement);
}, true);

Or, if you're a jQuery-lover:

或者,如果您是 jQuery 爱好者:

$('body').on('click', 'a', function(e) {
    e.target.target = '_blank';
});

回答by RippeR

yep, u can add attribute with JS to all links in HTML document named 'target' with value '_blank' ;)

是的,您可以使用 JS 将属性添加到名为“target”且值为“_blank”的 HTML 文档中的所有链接;)

You could also open replace href's of all links from url to javascript:openInWindow(url) using this, and writing function in JS that opens new window and set's it's location to url;) Google will help you with both.

您还可以使用this打开从 url 到 javascript:openInWindow(url) 的所有链接的替换 href ,并在 JS 中编写函数以打开新窗口并将其位置设置为url;) Google 将帮助您解决这两个问题。

回答by MJ Walsh

Just add a html baseelement to the page using Javascript:

只需base使用 Javascript 向页面添加一个 html元素:

var e = document.createElement("base");
e.target = "_blank";
document.head.appendChild(e);