2013-06-13 47 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

感谢您的评论。它工作正常 –