2013-06-13 46 views

回答

1

您可以使用選擇的a元素(使用jQuery),像這樣:

$("a").click(function(e) { 
    alert($(this).attr("href")); 
}); 
0

你可以做到這一點使用jQuery:

$(function() { 
    $("a").on('click', function() { 
     alert("Hello"); 
    }); 
}); 
1

使用純JavaScript(因爲這個問題沒有按根本沒有提到jQuery),你可以使用getElementsByTagName

var anchors = document.getElementsByTagName("a"); // get all <a> elements on the page 
for(var i=0; i< anchors.length; i++){ // for each one 
    anchors[i].onclick = function(){ // if clicked 
    alert("All anchors will trigger this on click"); // alert 
    } 
} 

jsFiddle here.

0
$(document).ready(function() { 

$("a").click(function() { 
     alert('............'); 
    }); 

}); 
+0

如果你需要動態創建鏈接,使用'on'。 –

1

使用jQuery:

$(document).on('click','a', function(e){ 
    alert($(this).attr("href")); 
}) 

這將工作在dinamicaly創建的鏈接了。

+0

感謝您的評論。它工作正常 –