阻止事件对象默认行为
Tutorial: DOM实战
Category: JS
Published: 2026-04-07 13:58:26
Views: 20
Likes: 0
Comments: 0
<!DOCTYPE html>
<html lang="en">
<head>
<style></style>
</head>
<body>
<div>123</div>
<a href="http://www.baidu.com">百度</a>
<form action="http://www.baidu.com">
<input type="submit" value="提交" name="sub" />
</form>
<script>
var div = document.querySelector("div");
div.addEventListener("click", fn);
div.addEventListener("mouseover", fn);
div.addEventListener("mouseout", fn);
function fn(e) {
console.log(e.type);
}
var a = document.querySelector("a");
a.onclick = function (e) {
return false;
alert(11);
};
var form = document.querySelector("form");
form.onsubmit = function (e) {
e.preventDefault();
console.log(e);
};
</script>
</body>
</html>