<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("a").click(function (event) {
event.preventDefault();
alert('disabled');
});
});
</script>
</head>
<body>
<a href="/">Go to dotnetheaven.com </a>
</body>
</html>
这里(http://api.jquery.com/event.preventDefault/),说的是:如果调用这个方法,则不会触发事件的默认 Action 。
问题:
对于上面的代码,事件意味着'点击'? “默认操作”是打开 URL 吗?我理解正确吗?因为我想知道为什么警报仍然出现在 event.preventDefault()
之后。
最佳答案
默认操作是打开 URL。
event.preventDefault()
将阻止这种情况。
alert()
不是默认操作,这是您指定的操作(因为您已将其放入点击事件处理程序中)。因此这仍然会起作用。
关于javascript - 试图理解 jquery 中的 event.preventDefault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17275633/