2013-01-21 22 views
5

我一直在尋找通過#1的問題,試圖讓一個簡單的鏈路時延的幫助;我想把它放在一個div上,而且我無法找到我找到的例子的頭或尾。我想延遲的鏈路,爲期500使用javascript

到目前爲止,我知道我需要停止HREF的原生功能,但我不知道該怎麼做。代碼對我來說依然非常陌生。幫幫我?

+0

你是什麼意思*「延遲鏈接」*? – VisioN

+1

你的意思是,當瀏覽器之前的鏈接用戶_clicks_導航到鏈接的頁面出現的時間延遲? – mxgr

+0

請在問題中更具體。其中大部分沒有多大意義。一些示例或片段會有所幫助 –

回答

10

設置你的href屬性爲href="javascript:delay('URL')"和JavaScript:

function delay (URL) { 
    setTimeout(function() { window.location = URL }, 500); 
} 
+0

這將會是一個非常愚蠢的問題,但是我在哪裏放置它以影響div? – ammonhra

+1

喔,如果這是一個div,然後使它

Click here to go to google in half a second
gurvinder372

+0

哦,等一下,我剛剛意識到要做些什麼。謝謝。 – ammonhra

3

如果你想耽誤您的網頁上的每一個環節,你可以用jQuery做這樣的

$(function(){ 
    $("a").click(function(evt){ 
     var link = $(this).attr("href"); 
     setTimeout(function() { 
      window.location.href = link; 
     }, 500); 
    }); 
}); 
+3

對於這個工作,你將需要添加evt.preventDefault();在你的功能開始。 –

+2

這不支持'target =「_ blank」'或任何其他'target'設置。 –

-1

我用這個來在繼續之前保持功能等待:

var wait_until = new Date().getTime() + 500; 
while (new Date().getTime() < wait_until) { 
    //Do nothing, wait 
}