【转】阻止浏览器发生默认行为

preventDefault方法可以阻止浏览器发生默认行为。

JS阻止链接跳转的实例: 

<script type="text/javascript">

function stopDefault(e) {
if (e && e.preventDefault) {//如果是FF下执行这个
e.preventDefault();
}else{
window.event.returnValue = false;//如果是IE下执行这个
}
return false;
}
</script>
<a href="http://www.cnblogs.com" id="test">测试</a>
<script type="text/javascript">
var test = document.getElementByIdx_x('test');
test.onclick = function(e) {
alert('URL:' + this.href + ', 不会跳转');
stopDefault(e);
}
</script>

 此时点击链接,不会跳转,只弹出一个对话框。

转自天山凌雪的博客

原文链接:http://www.cnblogs.com/ppoo/archive/2011/12/29/2306018.html

原文地址:https://www.cnblogs.com/xiaoyusmile/p/2362971.html