So, i have such problem: first time, then the <a> get clicked e.preventDefault() isn’t working and I jump to the top of the page, but on the second time it works. What is the problem?
$('#more a').live('click', function(e) {
var last = $('p.post').last().data('id');
console.log('start');
if (last != '1') {
$.ajax({
type: "POST",
url: "http://live.iappleworld.ru/ajax_more_mobile.php",
data: "last=" + last,
beforeSend: function() {
$('#more a').text('').prepend('<img src="http://live.iappleworld.ru/ajax-loader.gif" alt="loading"/> Loading...');
},
success: function(html) {
$("#more").remove();
$(html).hide().appendTo('ul.pageitem').slideDown(600);
}
});
}
e.preventDefault();
});
Before and after i have another script.
About
.on()Syntax of
.on()for delegate event handling:Where
containeris anyStatic elementwithin which thetargetelement belong and of course container should belong to DOM at page load i.e it should not dynamic.Read more aboue
.on()Note
Try to avoid
.live()if you have choice to usejQuery >= 1.7and bind delegate event with.on().According to comment
You’ve wrote
but it should be
Another option is change
ataghreftoand remove
e.preventDefault().