2015-11-25 43 views
-4

我想要點擊某個<a>時發出警告,但前提是存在真正的鏈接。我有這個代碼,但它並沒有真正的工作...如何在點擊鏈接時使用jQuery提醒?

$("a").click(function(){ 
    if ($(this).attr("href") != "#" && $(this).attr("href") != "") { 
     alert("foo"); 
    } 
}); 

什麼是錯?

感謝

+5

它怎麼不起作用? [這支筆](http://codepen.io/anon/pen/dYBomK)顯示這是按要求操作的。 –

+0

你如何定義「* real *」鏈接? –

+1

你把它包裝在'$(document).ready(function(){......})裏嗎? –

回答

0

確保你的方法調用e.preventDefault()

$("a").click(function(e){ 
    if ($(this).attr("href") != "#" && $(this).attr("href") != "") { 
     alert("foo"); 
     e.preventDefault(); 
    } 
}); 
相關問題