2012-08-29 55 views
2

使用jQuery我想先加载本地HTML文件(其中包含spesific id元素) 之后,我想运行一个jQuery插件来那是spesific id元素上工作从外部页面加载。jQuery的执行顺序问题

但预期这是行不通的。

下面你可以看到代码块预期这是行不通的。

$("#left").load("tree.html"); 

$("#tree").treeview({ 
     collapsed: false, 
     animated: "medium", 
     persist: "location" 
    }); 

* *一旦我谨tree.html的实际页面的内容。 treeview插件正在工作**确定!负载完成之前正在执行

回答

5

树视图。您可以使用load method的完整的回调来修复这个

$('#left').load('tree.html', function() { 
    $("#tree").treeview({ 
     collapsed: false, 
     animated: "medium", 
     persist: "location" 
    }); 
}); 

值得一提的是,在jQuery的大多数操作不会阻塞,因此,当你需要行动上的负载或AJAX请求时,最好使用提供的回调。

+0

优秀!我怎么能错过这个回调! –