In the following code e.preventdefault(); isn't working, the browser is visiting the anchors href anyway. I can see the first alert, but after that the browser follows the href link of the anchor. I've read this (similiar) question on Stackoverflow, but it isn't really helping me: e.preventdefault(); not working
Any ideas on what i'm doing wrong or how I can fix it? Thanks in advance.
$('body').delegate('a.newslink', 'click', function(e) {
alert('start');
e.preventDefault();
$.get( $(this).attr('href'), function(data) {
alert(data);
$('.reader').replaceWith(data);
$('#article-menu').removeClass('open');
$('#article-menu').addClass('closed');
$('#newslist').animate({height:'0px'}, 200);
});
});
Try making the preventDefault call the first thing you do. The alert might be getting in the way.
Also because this is a delegated event, you might need to call e.stopPropagation as well.
Finally, I am not 100% sure in this case, but the event may not be cancelable (check the value of e.cancelable). See https://developer.mozilla.org/en/DOM/event.preventDefault
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.