简体   繁体   English

带有.load的jQuery .html函数

[英]jQuery .html function with .load

I am loading HTML content using jQuery's .html() function. 我正在使用jQuery的.html()函数加载HTML内容。 Part of the HTML content I am trying to load are images, which take some time to be loaded. 我尝试加载的HTML内容的一部分是图像,这些图像需要一些时间才能加载。 What I do is on an onclick event, 我要做的是一个onclick事件,

$('div').on('click',function() {
    $('html').fadeOut(1000)
        .html(content)
        .load(function() {
            $('html').fadeIn(1000)
        });
});

What I wanted to happen is that, when the DOM has finished loading, I want it to fadeIn. 我想发生的是,当DOM完成加载后,我希望它褪色。 If it is still not finished, I want it to stay hidden hence, the fadeOut function before the html load. 如果还没有完成,我希望它保持隐藏状态,因此,在html加载之前使用fadeOut函数。

Is this possible? 这可能吗? That method doesn't seem to work for me. 该方法似乎对我不起作用。

First, if you are replacing the entire HTML element, you might as well just do a full request cycle. 首先,如果要替换整个HTML元素,则最好执行一个完整的请求周期。 That's essentially what you're going to end up with and it will be easier to hook to the window load event using a full request than to handle this with AJAX. 从本质上讲,这就是您要得到的结果,并且使用完整请求来挂钩窗口加载事件要比使用AJAX处理该事件要容易得多。 If you're not replacing the entire page, then you've got your selector wrong. 如果您不替换整个页面,则选择器有误。

Second, if you are just loading the DIV, then you can try hiding the DIV, binding the load event to it, then loading it with content. 其次,如果您只是加载DIV,则可以尝试隐藏DIV,将load事件绑定到它,然后再将其加载内容。 See the caveats for the http://api.jquery.com/load-event/ method for more information. 有关更多信息,请参见http://api.jquery.com/load-event/方法的警告。

$('div').on('click',function() {
    var $this = $(this); // save reference for future use
    $this.fadeOut(1000) // hide
         .load(function() { // hook up handler
             $this.fadeIn(1000)
         })
         .html(content);  // load content
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM