How to load html into a “skin” that has also been loaded from an external html file?

末鹿安然 提交于 2019-12-12 02:38:52

问题


I'm trying to load html into a skin (header/footer with nav buttons) that has also been loaded from an external html file. Here's the code in jsfiddle although it won't work because I can't put the two external files into jsfiddle:

http://jsfiddle.net/J4yUt/1/

This is the content in the two external files:

medbox.html:

<div id="medBox"></div>

smallBox.html:

<div id="smallBox"></div>

When I click the button, the medium box appears in the big box, but the small box doesn't appear in the medium box. I think the code to load the smallbox.html executes before the medBox.html has fully loaded, so how do I get it to wait for the medBox.html to fully load?

This is what I want the result look like:

http://jsfiddle.net/Sv4yf/2/


回答1:


$("#bigBox").load("medBox.html", function(){
   $("#medBox").load("smallBox.html");
});

Use a callback function in your first load handler.

Read more: http://api.jquery.com/load




回答2:


Just use .load()'s callback:

$("#bigBox").load("medBox.html", function(response, status, xhr) {
    if (status == 'success') {
        $("#medBox").load("smallBox.html");
    }
});

.load() sends an AJAX request, which is asynchronous and doesn't block the execution of code after it.



来源:https://stackoverflow.com/questions/12592203/how-to-load-html-into-a-skin-that-has-also-been-loaded-from-an-external-html-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!