javascript 在所有链接中定位 _blank

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24428476/
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-10-28 02:46:32  来源:igfitidea点击:

Target _blank in all Link

javascripthyperlinktarget

提问by Hammad

I've just made an html page with target _self

我刚刚制作了一个带有目标的 html 页面 _self

But now I have too many links and I want to change all link targets to _blankand it's hard for me to do. Is there is any javascript which apply on all just to write 1 time ??

但是现在我的链接太多了,我想将所有链接目标更改为_blank,这对我来说很难做到。是否有任何 javascript 适用于所有只写 1 次?

Because my code is too long and it is taking too many times to change in all links. Is there is any trick?

因为我的代码太长,而且所有链接都需要修改太多次。有什么技巧吗?

like this

像这样

<a href="http://www.google.com"></a>
<a href="http://www.facebook.com"></a>
<a href="http://www.gmail.com"></a>
<a href="http://www.twitter.com"></a>
<a href="http://www.stackoverflow.com"></a>
<a href="http://plus.google.com"></a>

回答by Thom Wiggers

Put this in your <head>

把这个放在你的 <head>

<base target="_blank">

It will make all URLs on a page open in a new page, unlesstargetis specified.

除非target指定,否则它将使页面上的所有 URL 在新页面中打开。

This is a HTML5-only feature, I learnt it from Google's io-2012-slides slide package.

这是一个仅限 HTML5 的功能,我是从Google 的 io-2012-slides 幻灯片包中学到

回答by Oliver Spryn

To answer your question, jQuery makes this easy:

为了回答您的问题,jQuery 让这一切变得简单:

<script src="//ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script>
    $(function() {
        $('a[href]').attr('target', '_blank');
    });
</script>

This will not modify any <a>tags without an hrefattribute.

这不会修改<a>没有href属性的任何标签。

回答by Matt Browne

It's pretty simple in plain JS too:

在普通 JS 中也很简单:

var links = document.getElementsByTagName('a');
for (var i=0, len=links.length; i < len; i++) {
  links[i].target = '_blank';
}

(Put this script right before your closing </body>tag or in any case after all the <a>tags on your page.)

(将此脚本放在结束</body>标记之前或在任何情况下<a>放在页面上的所有标记之后。)

回答by Fr0zenFyr

If you are unable to do a simple find and replaceusing your HTML editor, you can do something like this using jQuery:

如果您无法find and replace使用 HTML 编辑器进行简单的操作,您可以使用 jQuery 执行以下操作:

$('a').click(function() {
  $(this).attr('target', '_blank');
});

This will automatically do a target="_blank"for every athat is clicked and open in a new window or new tab(you have no control over this, it depends on user's browser settings).

这将自动target="_blank"为每个a点击并在新窗口或新选项卡中打开(您无法控制,这取决于用户的浏览器设置)执行。

FIDDLE

小提琴

Hope this helps!!

希望这可以帮助!!

回答by arielnmz

I've had to deal with this lotsof times. Just for the record:

我不得不处理这个很多次。仅作记录:

I think there's no need to make javascript perform this task, but here's another approach, instead you can use the find/replace function of your editor and do something like this (provided your links are in that format):

我认为没有必要让 javascript 执行此任务,但这是另一种方法,您可以使用编辑器的查找/替换功能并执行以下操作(前提是您的链接采用该格式):

  • Open your editor and look for <a href:

    Find <a href

  • On the replace field type <atarget="_blank"href.

    Replace

  • Replace.

    Text replaced

  • 打开您的编辑器并查找<a href

    查找 <a href

  • 在替换字段类型上。<atarget="_blank"href

    代替

  • 代替。

    文本已替换

Or you can append that to the end of the tag by looking for .com">and replacing it for .com" target="_blank">. You can do this on all editorsthat have the find/replacefeature. It's just a matter of finding patterns and how to replace them.

或者您可以通过查找.com">和替换它来将其附加到标签的末尾.com" target="_blank">。您可以在所有具有查找/替换功能的编辑器上执行此操作。这只是寻找模式以及如何替换它们的问题。