jquery html 回调 [英] jquery html callback
问题描述
是否有 html() 函数的回调或检查它是否已完成的方法.例如:
Is there a callback for the html() function or a way to check that it is finished. For example:
$("#some_div").html('something here');
if($("#some_div").html() == ''){
//do something here
}
我正在设置帖子中元素的 html.有时它不加载所以当它不加载时我希望它做一些事情,这可能吗.我现在拥有它的方式总是做另一件事,因为它在检查它有 html 时还没有设置 html.
I am setting the html of an element from a post. Sometimes it doesnt load so when it doesnt load I would like for it to do something, is this possible. The way I have it now it always does the other thing because it hasnt set the html by the time it checks that it has html.
推荐答案
html()
是一个同步操作.DOM 的实际更新取决于您的 html 内容是什么.如果您有 或
标签,它们将需要一些时间来加载.
html()
后面的下一条语句应该立即看到新的 html 内容.
html()
is a synchronous operation. The actual updating of the DOM depends on what your html content is. If you have <img>
or <iframe>
tags, they will take time to load. The next statement following the html()
should immediately see the new html contents.
你是说 load()
吗?
[Edit] 根据您的评论,可能是您的 $.ajax
失败了.不是 html()
.尝试将故障处理程序附加到 ajax 调用并从那里重试?虽然,如果您的服务器端代码以 200 代码静默失败,则不会调用失败事件.在这种情况下,您可以在将 html 值设置为元素之前对其进行验证.
Based on your comment, it is probably your $.ajax
failing. Not html()
. Try attaching a failure handler to ajax call and retry from there? Although, if your server side code silently fails with a 200 code, the failure event doesn't get called. In that case, you can validate the html value before you set it to the element.
这篇关于jquery html 回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!