當我點擊example4中的「Google」鏈接時,我收到了2條警告消息[它也顯示了alert3消息]。請給出一個解決方案,嘗試區分example4中的鏈接,但沒有結果。我收到2條警報消息。如何區分jquery中的鏈接?
這段代碼有什麼問題,我是jquery世界的新手。
jQuery代碼:
//Example 03 - Prevent Default Action of HTML Element
$(document).ready(function(){
$("a").click(function(e){
e.preventDefault();
alert("Example 3 :: The default action of anchor element has been prevented using Jquery");
});
});
//Example 04 - Is Default Action Prevented of HTML Element
$(document).ready(function(){
$("a.checkaction").click(function(event){
event.preventDefault();
alert("Example4 :: Is default action prevented of anchor <a>element :: "+event.isDefaultPrevented());
});
});
HTML代碼:
<!-- Jquery example 03 - Prevent default behavior of HTML element using Jquery -->
<h1 class="header">Example 3 - Prevent default behavior of HTML Element</h1>
<a href="http://www.facebook.com">Facebook</a>
<!-- Jquery example 04 - How to check whether the default action oh html is prevented or not using jquery -->
<h1 class="header">Example 4 - How to check whether the default action oh html is prevented or not</h1>
<a href="http://www.google.com" class="checkaction">Google</a>
此代碼工作正常。「返回false」是什麼意思? – Arjunan
在這個例子中,'return false;'防止元素的默認行爲。處理表單和jQuery時常用。 'preventDefault()'同樣的事情本質上 - 但少代碼。 –
謝謝Mike Barwick。 – Arjunan