javascript - jQuery losing $(this) object after DOM update -
html
<ul> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> js
$('ul').on('hover', 'li', function(){ // ajax call data // , put response data current li html $(this).html(data); // here $(this) lost if both event fire @ same time , dom updated via second ajax response }); $( window ).scroll(function() { // ajax call next content , append list $( "ul" ).append( data ); }); i loading data via ajax.
there 2 events (1) hover on li , (2) scroll
if user fires both events @ same time , response of scroll ajax call comes first elements dom appended, , response of second ajax call came, , getting $(this) lost in success callback.
seems context:this, should used in ajax call, if using jquery.ajax() method can add context in options of it:
$.ajax({ url:'', ...... context: this, ...... }); if use context context provided in available in callbacks of ajax call.
Comments
Post a Comment