我開始使用jquery,並在官方網站上找到了教程。 http://docs.jquery.com/How_jQuery_Works#jQuery:_The_Basicsjquery事件處理鏈接
我在標有文檔就緒啓動代碼的章節中。如果你注意到,他們提供了兩個例子。在將您帶到jQuery網站之前會彈出一個警告框,另一個警告框會阻止您前往該網站。
假設我想要兩個鏈接。一個出現警告框並點擊「確定」後會進入jquery的網站,另一個出現警告框但阻止您進入jquery的網站。我只想爲不同的鏈接找出不同的迴應。我需要給它某種類型的ID嗎?
這是代碼。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
</head>
<body>
<script src="jquery.js"></script><br/>
<a href="http://jquery.com/" id="1">jQuery</a><br/> <!--first link: will display message and then proceed to site -->
<script>
$(document).ready(function(){
$("a#1").click(function(event){
alert("Thanks for visiting!");
});
});
</script>
<a href="http://jquery.com/" id="2">jQuery</a> <!-- second link: message appears and does not continue to site -->
<script>
$(document).ready(function(){
$("a#2").click(function(event){
alert("As you can see, the link no longer took you to jquery.com");
event.preventDefault();
});
});
</script>
編輯 - 添加標識對錨。謝謝你們,它有效。
是的,現在,您將'click()'附加到頁面上的每個''標籤。你必須使用選擇器來定位你需要的''。這可以通過給予每個''一個單獨的類或id來完成。 – Nadh