Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在新窗口中打开链接后,如何使用JavaScript更改href属性?_Javascript_Html - Fatal编程技术网

在新窗口中打开链接后,如何使用JavaScript更改href属性?

在新窗口中打开链接后,如何使用JavaScript更改href属性?,javascript,html,Javascript,Html,我的页面上有一个链接 <a href="http://google.com" id="mylink" onclick="changeLink();" target="_blank">google</a> 但它也改变了新打开选项卡的目标。在我的例子中,它没有打开谷歌,而是打开了facebook 可以修复它吗?您可以在单击后使用setTimeout延迟代码执行 function changeLink(){ setTimeout(function() {

我的页面上有一个链接

<a href="http://google.com" id="mylink" onclick="changeLink();" target="_blank">google</a>
但它也改变了新打开选项卡的目标。在我的例子中,它没有打开谷歌,而是打开了facebook


可以修复它吗?

您可以在单击后使用
setTimeout
延迟代码执行

function changeLink(){
    setTimeout(function() {
        var link = document.getElementById("mylink");
        link.setAttribute('href', "http://facebook.com");
        document.getElementById("mylink").innerHTML = "facebook";
    }, 100);
}
替换

onclick="changeLink();"


要取消其默认操作

您的
onclick
在href启动之前启动,以便在打开页面之前更改,您需要使函数按如下方式处理窗口打开:

function changeLink() {
    var link = document.getElementById("mylink");

    window.open(
      link.href,
      '_blank'
    );

    link.innerHTML = "facebook";
    link.setAttribute('href', "http://facebook.com");

    return false;
}

您可以在页面
load
中对此进行更改

我的意图是,当页面进入加载功能时,切换链接(所需链接中的当前链接)

例如,尝试以下操作:

<a href="http://www.google.com" id="myLink1">open link 1</a><br/> <a href="http://www.youtube.com" id="myLink2">open link 2</a>

document.getElementById(“myLink1”).onclick=function(){ 打开窗户( "http://www.facebook.com" ); 返回false; }; document.getElementById(“myLink2”).onclick=function(){ 打开窗户( "http://www.yahoo.com" ); 返回false; };
利用
mousedown
listener使用新的URL位置修改
href
属性,然后让浏览器确定它应该重定向到哪里,这有什么坏处吗

到目前为止对我来说还不错。想知道这种方法的局限性是什么吗

// Simple code snippet to demonstrate the said approach
const a = document.createElement('a');
a.textContent = 'test link';
a.href = '/haha';
a.target = '_blank';
a.rel = 'noopener';

a.onmousedown = () => {
  a.href = '/lol';
};

document.body.appendChild(a);
}
<a href="http://www.google.com" id="myLink1">open link 1</a><br/> <a href="http://www.youtube.com" id="myLink2">open link 2</a>
document.getElementById("myLink1").onclick = function() { window.open( "http://www.facebook.com" ); return false; }; document.getElementById("myLink2").onclick = function() { window.open( "http://www.yahoo.com" ); return false; };
// Simple code snippet to demonstrate the said approach
const a = document.createElement('a');
a.textContent = 'test link';
a.href = '/haha';
a.target = '_blank';
a.rel = 'noopener';

a.onmousedown = () => {
  a.href = '/lol';
};

document.body.appendChild(a);
}