2016-08-30 74 views
0

我有一個JavaScript函數,使用它禁用了一個超鏈接,它可以實現這個功能。現在幾秒鐘​​後讓我們說4秒後我想啓用鏈接。我不知道如何做到這一點。我寫了一個函數來啓用鏈接,但它不起作用。使用JavaScript啓用href

有人可以請幫助使用JavaScript。

JS功能

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

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

可能更容易隱藏/顯示具有元素'href'屬性 – Patrick

+0

disableDownloadReportLink如何被調用? –

+0

只需設置一個計時器,請參閱此處的示例http://stackoverflow.com/questions/21721159/how-to-set-timer-on-body-onload – Guenther

回答

0

添加一個類.inactive你將綁定一個單擊事件到e.preventDefault();

4秒後,刪除班級。

0

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>

基本上你有disableDownloadReportLink做的是禁用什麼onclick事件一樣。如果你希望鏈接再次正常工作,你所能做的就是設置onclickundefined

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

您可以在4秒後放調用enableDownloadReportLinksetTimeout爲了做到這一點:

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

你對如何調用這些函數的問題,這是我會做什麼,我告訴你,在我的評論(不要指望這個片段的工作,僅僅是一個例子):

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>

+0

然後你會怎樣稱呼這些功能? – Jaykumar

+0

我想你已經擁有了你需要的一切。你如何稱呼這些功能取決於你。你爲什麼不調用一個完成所有工作的函數(當你點擊鏈接時),而不是將鏈接的onclick函數調用到三個函數中?這個新函數將執行'submit()',然後調用函數來禁用鏈接,然後(在4秒後,使用'setTimeout')將調用該函數來啓用鏈接。 –

+0

它的投擲錯誤。 – Jaykumar

2

基本上,同樣的模式@Vicente Olivert裏埃拉,只是不同的實現,使用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>