如何检查元素是否已通过AJAX加载?
我想检查元素是否已加载。
HTML
JS
$(document).on('click','button',function () { $.ajax({ url: 'additional.html', context: document.body, }).done(function(html) { $('body').append(html); }); }); //My incorrect suggestion if ($('input').is(':visible')) { alert('I see loaded element!'); }
我可以将警报移动到.done()
块, 但我不允许更改它。 那么我应该将什么事件监听器用于if
语句,以便在元素出现时显示alert
?
你可以这样做:
$('body').bind("loaded", function () { alert('I see loaded element!'); }); $(document).on('click','button',function () { $.ajax({ url: 'additional.html', context: document.body, }).done(function(html) { $('body').append(html); $('body').trigger("loaded"); }); });
或者您可以将jquery ajax设置为同步,因为它默认为async。