首页 > 技术文章 > js为链接绑定点击事件并且附带return false;来阻止跳转

xupeiyu 2014-09-29 16:20 原文

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>
        <style>
         
        </style>
    </head>
    <body>
        <a href="http://www.baidu.com" target="_blank">百度跳转</a>
        <a href="http://www.baidu.com" target="_blank" id="test">阻止默认事件</a>
        <script>
            document.getElementById('test').onclick = function(e){
                e = e || window.event;
                if (e.preventDefault) {
                    e.preventDefault();
                } else {
                    e.returnValue = false;
                }
            }
        </script>
    </body>
</html>            

  

推荐阅读