Reputation: 3037
<!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>
Here(http://api.jquery.com/event.preventDefault/), it is said: If this method is called, the default action of the event will not be triggered.
Question:
For the above code, the event means 'click'? 'the default action' is openning the URL? Am I understanding correctly? Because I wonder why alert still shows up after event.preventDefault()
.
Upvotes: 1
Views: 170
Reputation: 103358
The default action is to open the URL.
event.preventDefault()
will prevent this.
The alert()
is not the default action, this is one you have specified (as you've put it in the click event handler). Therefore this will still action.
Upvotes: 2
Reputation: 44740
The default behaviour is to change location.href
to /
which is being prevented using .preventDefault()
, Your event handler will continue to execute after preventing the default action.
Upvotes: 1
Reputation: 342635
For the above code, the event means 'click'? 'the default action' is openning the URL? Am I understanding correctly?
Yup.
Because I wonder why alert still shows up after event.preventDefault().
The rest of the function will still execute, event.preventDefault()
does not return.
Upvotes: 2