简体   繁体   English

在同一新标签页中打开外部链接

[英]Opening external links in the same new tab

How to open external links in the same new tab in the same browser window ? 如何在同一浏览器窗口的同一新选项卡中打开外部链接?

( ie first click in an external link triggers a new tab, but subsequent clicks on the external links open up in that same tab without creating additional tabs in the browser window ) (即,第一次单击外部链接会触发一个新选项卡,但是随后对外部链接的单击会在同一选项卡中打开,而不会在浏览器窗口中创建其他选项卡)

Example : See how LegWorkStudio doing it in their portfolio in between the following links 示例:在以下链接之间查看LegWorkStudio如何在其投资组合中做到这一点

  1. http://www.legworkstudio.com/interactive/laika-the-boxtrolls http://www.legworkstudio.com/interactive/laika-the-boxtrolls
  2. http://www.legworkstudio.com/interactive/kentucky-fried-chicken http://www.legworkstudio.com/interactive/kentucky-fried-chicken
  3. http://www.legworkstudio.com/interactive/newton-run-better http://www.legworkstudio.com/interactive/newton-run-better
  4. http://www.legworkstudio.com/interactive/coke-ahh-effect http://www.legworkstudio.com/interactive/coke-ahh-effect

HTML : HTML:

<div id="target" />
<div id="source"><p>bla bla bla</p></div>

Content replacement via normal javascript : 通过普通javascript进行内容替换:

document.getElementById("target").innerHTML = document.getElementById("source").innerHTML;

Content replament via jQuery : 通过jQuery替换内容:

$("#target").html($("#source").html());

You can bind this code to a link by adding the onclick method to the link and wrapping it in a function (normal JS) or via the different methods of jQuery (on, click). 您可以通过将onclick方法添加到链接并将其包装在函数(普通JS)中,或通过jQuery的其他方法(单击on)将代码绑定到链接。

Example JS: JS示例:

<a id="copytext" href="#" onclick='copyText();return false'>Copy text</a>
<script>
    function copyText() {
        document.getElementById("target").innerHTML = document.getElementById("source").innerHTML;
    }
</script>

Example jQuery : jQuery示例:

<script>
$(function() {
    $("#copyref").click(function(e) {
        e.preventDefault();
        $("#target").html($("#source").html());
    });
})
</script>

It seems they just change window.location variable onclick then operates some JS code to display the right section. 看来他们只是更改window.location变量onclick然后操作一些JS代码以显示右侧部分。

At load, they surely read the window.location last argument and display the right section. 在加载时,他们一定会读取window.location最后一个参数并显示右侧部分。

You can make something like that : 你可以做这样的事情:

var myButton = document.getElementById("sectionXXXButton");
myButton.onclick = function(){
    var a = window.location.split("/");
    a[a.length-1] = "XXX";
    window.location = a.join("/");
    // Some code to undisplay / display appropriate sections
}

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

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