我想确保加载外部HTML后出现函数

时间:2013-05-14 17:03:29

标签: javascript jquery

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

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

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

非常感谢!

更新

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

2 个答案:

答案 0 :(得分:3)

您的代码是对的。

来自jquery documentation

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.

答案 1 :(得分:0)

你正在加载iFrames吗?

尝试.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.
});