Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript Event 对象 preventDefault() #14

Open
ClarenceC opened this issue Jun 1, 2017 · 0 comments
Open

JavaScript Event 对象 preventDefault() #14

ClarenceC opened this issue Jun 1, 2017 · 0 comments

Comments

@ClarenceC
Copy link
Owner

Event.preventDefault()

在事件里头取消事件的默认动作。
例如下面例子,点击连接不会跳转,只会弹出警告.

<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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant