简体   繁体   English

在所有链接中定位 _blank

[英]Target _blank in all Link

I've just made an html page with target _self我刚刚用目标_self制作了一个 html 页面

But now I have too many links and I want to change all link targets to _blank and it's hard for me to do.但是现在我的链接太多了,我想将所有链接目标更改为_blank ,这对我来说很难做到。 Is there is any javascript which apply on all just to write 1 time ??是否有任何 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>

Put this in your <head>把它放在你的<head>

<base target="_blank">

It will make all URLs on a page open in a new page, unless target is 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 幻灯片包中学到

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 href attribute.这不会修改任何没有href属性的<a>标签。

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>标签之后。)

If you are unable to do a simple find and replace using your HTML editor, you can do something like this using jQuery:如果您无法使用 HTML 编辑器进行简单的find and replace ,您可以使用 jQuery 执行以下操作:

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

This will automatically do a target="_blank" for every a that 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!!希望这可以帮助!!

I've had to deal with this lots of 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 :打开您的编辑器并查找<a href

    查找 <a href

  • On the replace field type <a target="_blank" href .在替换字段中输入<a target="_blank" href

    代替

  • Replace.代替。

    文本已替换

Or you can append that to the end of the tag by looking for .com"> and replacing it for .com" target="_blank"> .或者您可以通过查找.com">并将其替换为.com" target="_blank">将其附加到标签的末尾。 You can do this on all editors that have the find/replace feature.您可以在所有具有查找/替换功能的编辑器上执行此操作。 It's just a matter of finding patterns and how to replace them.这只是寻找模式以及如何替换它们的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM