DIV CSS 佈局教程網

用 jQuery 阻止 a 標籤的鏈接跳轉
編輯:JQuery特效代碼     

在做單頁應用或需要自定義點擊行為時,我們常常要阻止 <a> 標籤的默認跳轉。透過在 click 事件中返回 false 即可輕鬆實現:

<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- 引入 jQuery -->
<script src="https://hovertree.com/ziyuan/jquery/jquery-1.12.1.min.js" type="text/javascript"></script>
<script>
$(function(){
  $("a").click(function(event) {
    alert(event.type); //獲取事件類型
    return false; //阻止鏈接跳轉
  });
})
</script>
</head>
<body>
<a href='https://hovertree.com'>click me .</a>
</body>
</html>

關鍵就在 return false; 這一行——它會同時阻止默認行為(跳轉)與事件冒泡。效果如下,請點擊鏈接測試:

click me .

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved