在新选项卡中打开链接 [英] Opening a link in a new tab

查看:129
本文介绍了在新选项卡中打开链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我正在做的一个项目创建了一个网站。
在网站的内容中有一些可以访问的外部网页的链接。
在用户点击其中一个链接的同时,他将被带到指定的链接,他将不再在当前页面上。
我想要做的是当用户点击链接时,点击链接中的指定网站会出现在新标签页中。通过这种方式,用户可以停留在当前页面上,也可以在新选项卡上查看其他页面。



我在互联网上看过并发现:b
$ b

  function externalLinks()
{
var anchors = document.getElementsByTagName(a );
for(var i = 0; i< anchors.length; i ++)
{
var anchor = anchors [i];
if(anchor.getAttribute(href))
anchor.target =_blank;
}
}
window.onload = externalLinks;

我面对的问题是我的网站的导航栏包含锚标签。所以,现在如果用户点击导航栏上的链接,它会打开一个新的标签。我希望只有当用户点击我网站内容中的链接时才会发生这种情况。因此,如果用户点击导航栏中的链接,它不应该打开一个新的标签,并应该带他到指定的目的地。



我尝试添加一个类到内容中的所有链接并使用getElementByClassName,但它仍然无法工作



任何人都可以帮助我完成此操作

 < a target =_ blankhref =YourAmazingURL>点击此处获取惊人的网址< / a> 

另一个例子:

 < a target =_ blankhref =http://www.google.com/> Google< / a> 

这利用了目标属性。



<有关目标属性的更多信息: http://www.w3schools.com/tags/att_a_target。 asp
另外: http://www.w3schools.com/html/ html_links.asp



编辑:



对于XHTML,只需执行以下操作:

 < a href =YourAmazingURLonclick =window.open(this.href,'_ blank'); return false;>点击此处获取惊人的URL< / a> 

还有:


 < a href =http://www.google.com/onclick =window.open(this.href,'_ blank'); return false; >谷歌< / A> 





I've created a website for a project I'm doing. In the content of the website there are some links for external web pages that can be visited. In the mean time when the user clicks on one of the links he'll be taken to the specified link and he will not be on the current page anymore. What I wanted to do is have the specified website in the link clicked appear in a new tab when the user clicks on the link. This way the user stays on the current page he's one and also can view the other page on the new tab.

I've looked on the internet and found this which seemed to be useful:

function externalLinks()
{
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors.length; i++)
  {
      var anchor = anchors[i];
      if(anchor.getAttribute("href"))
        anchor.target = "_blank";
  }
}
window.onload = externalLinks;

The problem I'm facing is that the navbar of my website contains anchor tags. So now if the user clicks on the links on the navbar it will open a new tab. I want this to happen ONLY if the user clicks on a link in the content of my website. So if the user clicks on a link in the navbar it shouldn't open a new tab and should just take him to the specified destination.

I've tried adding a class to all the links in the content and use getElementByClassName but it still didn't work

Anyone can help me with this

解决方案


You can just use HTML:

<a target="_blank" href="YourAmazingURL">Click here for Amazing URL</a>

Another Example:

<a target="_blank" href="http://www.google.com/">Google</a>

This takes advantage of the target attribute.

More information on the target attribute: http://www.w3schools.com/tags/att_a_target.asp Also: http://www.w3schools.com/html/html_links.asp

EDIT:

For XHTML, just do this:

<a href="YourAmazingURL" onclick="window.open(this.href,'_blank');return false;">Click here for Amazing URL</a>

Or, again:

<a href="http://www.google.com/" onclick="window.open(this.href,'_blank');return false;">Google</a>


这篇关于在新选项卡中打开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆