您的位置:首页 > 其它

阻止标签的默认行为发生

2017-03-06 16:34 183 查看
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS阻止链接跳转</title>
<script type="text/javascript">
function stopDefault( e ) {
if ( e && e.preventDefault )
e.preventDefault();
else
window.event.returnValue = false;

return false;
}
</script>
</head>
<body>
<a href="http://www.baidu.com" id="testLink">百度</a>
<script type="text/javascript">
var test = document.getElementById('testLink');
test.onclick = function(e) {
alert('我的链接地址是:' + this.href + ', 但是我不会跳转。');
stopDefault(e);
}
</script>
</body>
</html>

 preventDefault方法就是可以阻止它的默认行为的发生而发生其他的事情

stopPropagation()是用来阻止事件冒泡 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: