使用JavaScript禁用/启用锚标记

时间:2022-11-27 17:59:57

I am trying to enable an anchor element using JavaScript but it looks like I am having some syntax issues. The disable part works fine but the anchor is not getting enabled after a time interval.

我试图使用JavaScript启用一个锚元素,但看起来我有一些语法问题。禁用部分工作正常,但在一段时间间隔后锚没有启用。

Form:

<a id="downloadReportLink"
   href="#x"
   title="This function will provide you a 30 day download of all your eSign transactions."
   onclick="document.getElementById('viewIntegrationReport').submit(), doEverything(this)">

   <span>Export E-Sign Information</span>
</a>

JS functions:

function disableLink(link) {
    link.onclick = function(event) {
        event.preventDefault();
    }
}

function enableLink(link) {
    link.onclick = undefined;
}

function doEverything(link) {
    disableLink(link);
    setTimeout(function() {
            enableLink(link);
        },
        4000
    );
}

I assumed link.onclick = undefined would enable the anchor but it does not.

我假设link.onclick = undefined会启用锚点,但它不会。

Am I doing anything wrong here?

我在这做错了吗?

2 个解决方案

#1


1  

It's a little unclear what behavior you want, but assuming you want to restore the original onclick of the anchor tag after the four-second interval, I think this will work for you. You were setting link.onclick to undefined, but you really want to set it back to what it was before.

有点不清楚你想要什么行为,但假设你想在四秒间隔之后恢复锚标签的原始onclick,我认为这对你有用。您将link.onclick设置为undefined,但您确实希望将其设置回原来的状态。

(I'm also assuming that you're intercepting the form submit and not actually navigating away from the page.)

(我还假设您正在拦截表单提交,而不是实际导航离开页面。)

function disableLink(link) {
    link.onclick = function(event) {
        event.preventDefault();
    }
}

function enableLink(link, onclick) {
    link.onclick = onclick;
}

function doEverything(link) {
    var oldOnclick = link.onclick;
    disableLink(link);
    setTimeout(function() {
        enableLink(link, oldOnclick);
    }, 4000);
}
<a id="downloadReportLink"
   href="#x"
   title="This function will provide you a 30 day download of all your eSign transactions."
   onclick="console.log('SUBMITTING'); doEverything(this); return false;">

   <span>Export E-Sign Information</span>
</a>

#2


-1  

Try this:

function disableLink(link) {
link.addEventListener("click", function(){
return false;
});

 }

function enableLink(link) {
link.addEventListener("click", function(){
return true;
});

#1


1  

It's a little unclear what behavior you want, but assuming you want to restore the original onclick of the anchor tag after the four-second interval, I think this will work for you. You were setting link.onclick to undefined, but you really want to set it back to what it was before.

有点不清楚你想要什么行为,但假设你想在四秒间隔之后恢复锚标签的原始onclick,我认为这对你有用。您将link.onclick设置为undefined,但您确实希望将其设置回原来的状态。

(I'm also assuming that you're intercepting the form submit and not actually navigating away from the page.)

(我还假设您正在拦截表单提交,而不是实际导航离开页面。)

function disableLink(link) {
    link.onclick = function(event) {
        event.preventDefault();
    }
}

function enableLink(link, onclick) {
    link.onclick = onclick;
}

function doEverything(link) {
    var oldOnclick = link.onclick;
    disableLink(link);
    setTimeout(function() {
        enableLink(link, oldOnclick);
    }, 4000);
}
<a id="downloadReportLink"
   href="#x"
   title="This function will provide you a 30 day download of all your eSign transactions."
   onclick="console.log('SUBMITTING'); doEverything(this); return false;">

   <span>Export E-Sign Information</span>
</a>

#2


-1  

Try this:

function disableLink(link) {
link.addEventListener("click", function(){
return false;
});

 }

function enableLink(link) {
link.addEventListener("click", function(){
return true;
});