如何检查元素是否已经通过AJAX加载? [英] How to check if element is already loaded via AJAX?
本文介绍了如何检查元素是否已经通过AJAX加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要检查元素是否已经加载.
I want to check if element is already loaded.
HTML
<button>load</button>
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
?
I could move alert to .done()
block, but I am not allowed to change it.
So what event listener should I use for if
statement in order to show alert
when element appears?
推荐答案
您可以执行以下操作:
$('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设置为同步,因为默认情况下它是异步的.
Or you can set jquery ajax to be sync, because it async as default.
这篇关于如何检查元素是否已经通过AJAX加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文