0

如何确保在加载外部 HTML 之后发出警报?

function changeContent(){
  $('#contentmain').load("contentmain.html", function(){
    alert("something");
}
)}

我一直在玩 $(document).ready,但到目前为止还没有运气。

非常感谢!

更新:

这段代码的结果是它取决于(取决于什么,我不知道):有时警报首先出现,有时它出现在第二个......

4

2 回答 2

3

你的代码是对的。

jquery 文档

Callback Function

If a "complete" callback is provided, it is executed after post-processing and HTML insertion has been performed. The callback is fired once for each element in the jQuery collection, and this is set to each DOM element in turn.
于 2013-05-14T17:05:12.963 回答
0

你在加载 iFrame 吗?

试试.load()函数。

$('#iframeID').load(function(){
    // I am totally loaded and lets begin the hunt now.
});

或者,如果您通过 ajax 加载内容,您可以使用.ajaxComplete

$(document).ajaxComplete(function(){
    // ajax call has completed and lets begin the hunt now.
});
于 2013-05-14T17:12:37.670 回答