找到你要的答案

Q:Changing href attribute on click event and then revert it back immediately?

Q:改变href属性单击事件,然后立即回复吗?

How can I change the "href" attribute af a link (A) element with JavaScript in its onclick event handler, and revert back to the original href after the click happened? So when second click happens the original href url should open.

My approach is the following using setTimeout: enter code herehttp://jsfiddle.net/6u4zvvy9/

Can it be solved without setTimeout?

我怎样才能改变“href”属性的链接(一)AF在onclick事件处理程序脚本元素,并恢复原来的链接点击后发生了什么?所以当二点击原链接的URL应该开放。

My approach is the following using setTimeout: enter code herehttp://jsfiddle.net/6u4zvvy9/

它可以解决没有setTimeout?

answer1: 回答1:

so maybe just record the original href:

elem = document.getElementById("google");
elem.newhref = "http://yahoo.com"
elem.originhref = "";
elem.onclick = function () {
    if(elem.originhref){
        elem.href = elem.originhref;
        elem.onClick=null;
    }else{
        elem.originhref = elem.href;
        elem.href = elem.newhref;
    }
};

By the way, I think using timeout is quite a nice solution.

也许只是记录的原始链接:

elem = document.getElementById("google");
elem.newhref = "http://yahoo.com"
elem.originhref = "";
elem.onclick = function () {
    if(elem.originhref){
        elem.href = elem.originhref;
        elem.onClick=null;
    }else{
        elem.originhref = elem.href;
        elem.href = elem.newhref;
    }
};

顺便说一下,我认为使用超时是一个相当不错的解决方案。

javascript  events  onclick  settimeout