2013-09-21 47 views
-2

我有一個更復雜的重定向請求。當它點擊一個鏈接時,我希望在鏈接打開之前延遲2秒,但是我希望這適用於在頁面上單擊的任何鏈接。當前重定向的問題是您必須指定一個URL,我希望在單擊任何鏈接時應用時間延遲,然後重定向到該特定鏈接。HTML時間延遲重定向

您必須指定一個帶有當前URl元標記的着陸URL,該標記不會工作,因爲我需要它指向任何被點擊的鏈接。

回答

2

那麼你可以做到這一點是這樣的:

document.addEventListener('click', function(e) { // when the document is clicked 
    if (e.target.tagName === "A") {    // check if target was an anchor 
     e.preventDefault();      // if so prevent default behaviour 
     var url = e.target.href;     // save the url 
     setTimeout(function() {     // queue up a timeout that 
      window.location = url;    // sets the location after 
     }, 2000);         // two seconds 
    } 
}, false); 

注意addEventListener()不與IE < 9兼容,但也有其ways to deal with that我將離開你做你自己。

+0

真棒明天會嘗試! –