简体   繁体   English

使用JavaScript启用href

[英]Enable href using JavaScript

I have a JavaScript function using which I am disabling a hyperlink and it does the trick. 我有一个JavaScript函数,通过它我可以禁用超链接,并且可以解决问题。 Now after a few seconds lets say after 4 secs I want to enable the link. 现在,几秒钟后,可以说4秒钟后,我要启用链接。 I am not sure how to do this. 我不确定该怎么做。 I've written a function to enable the link but it is not working. 我已经编写了一个启用链接的功能,但是它不起作用。

Can someone please help using JavaScript. 有人可以帮忙使用JavaScript吗?

JS functions JS功能

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

 function enableDownloadReportLink() {
     document.getElementById('downloadReportLink').href.disabled = false;
   }   

Basically, the same pattern as @Vicente Olivert Riera , just a different implementation, using add/remove EventListener ... 基本上,与@Vicente Olivert Riera相同的模式,只是不同的实现,使用add/remove EventListener ...

<a href="./foo" id="my-link">foo</a>

<script>
function disableLink(link) {
    var handler = function(e) {
        e.preventDefault();
        console.log('click disabled');
    }
    link.addEventListener('click', handler, false);
    link.dataset.disableHandler = handler;
}
function enableLink(link) {
    if (link.dataset.disableHandler) {
        link.removeEventListener('click', link.dataset.disableHandler);
        link.dataset.disableHandler = null;
    }
}

var link = document.getElementById('my-link');
disableLink(link);
link.style.color = 'grey';
console.log('link disabled');

setTimeout(function(){
    enableLink(link);
    link.style.color = 'blue';
    console.log('link enabled');
}, 4000);
</script>

You need to set aa timer in the on load funktion. 您需要在载入功能中设置一个计时器。 This has been asked before, see here for an example How to set timer on body onload? 之前已经问过这个问题,请参见此处的示例如何设置身体onload的计时器?

Add a class .inactive on which you would bind a click event to e.preventDefault(); 添加一个.inactive类,您可以在其上将click事件绑定到e.preventDefault();。

After 4 sec, remove the class. 4秒后,删除课程。

 function disableDownloadReportLink(link) { link.onclick = function(event) { event.preventDefault(); } } function enableDownloadReportLink(link) { link.onclick = undefined; } var el = document.getElementById("wop"); disableDownloadReportLink(el); setTimeout( function() { enableDownloadReportLink(el); }, 4000 ); 
 <a id="wop" href="http://www.google.com">I will work after 4 seconds!</a> 

Basically what you have done with disableDownloadReportLink is to disable what the onclick event does. 基本上,您使用disableDownloadReportLink完成的disableDownloadReportLink是禁用onclick事件的操作。 What you can do if you want the link to work normally again is to set onclick to undefined : 如果您希望链接再次正常工作,您可以将onclick设置为undefined

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

You can put the call to enableDownloadReportLink into a setTimeout in order to do it after 4 seconds: 您可以将enableDownloadReportLink的调用置于setTimeout中,以便在4秒后执行:

setTimeout(
  function() {
    enableDownloadReportLink(link);                                                                                                                              
  },
  4000
);

For your question about how to call those functions, this is what I would do, as I told you in my comment (don't expect this snippet to work, is only an example): 关于您如何调用这些函数的问题,正如我在评论中告诉您的那样,这就是我要做的(不要指望此代码片段仅是一个示例):

 function doEverything(link) { // submit integration report document.getElementById('viewIntegrationReport').submit(); // disable link disableDownloadReportLink(link); // enable link after 4 seconds setTimeout( function() { enableDownloadReportLink(link); }, 4000 ); } 
 <a href="#x" id="downloadReportLink" title="This function will provide you a 30 day download of all your eSign transactions." onclick="doEverything(this);"><span>Export E-Sign Information</span></a> 

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

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