jquery - 等待所有图像加载 jquery ajax/jquery pjax

标签 jquery ajax pjax

这是我正在尝试做的事情的片段

$.pjax = function( options ) {
  var $container = $(options.container),
      success = options.success || $.noop

  // We don't want to let anyone override our success handler.
  delete options.success

  // We can't persist $objects using the history API so we must use
  // a String selector. Bail if we got anything else.
  if ( typeof options.container !== 'string' )
    throw "pjax container must be a string selector!"

  var defaults = {
    timeout: 650,
    push: true,
    replace: false,
    // We want the browser to maintain two separate internal caches: one for
    // pjax'd partial page loads and one for normal page loads. Without
    // adding this secret parameter, some browsers will often confuse the two.
    data: { _pjax: true },
    type: 'GET',
    dataType: 'html',
    beforeSend: function(xhr){
      $container.trigger('start.pjax')
      xhr.setRequestHeader('X-PJAX', 'true')
    },
    error: function(){
      window.location = options.url
    },
    complete: function(){


$container.trigger('end.pjax')
},
success: function(data){
  // If we got no data or an entire web page, go directly
  // to the page and let normal error handling happen.
  if ( !$.trim(data) || /<html/i.test(data) )
    return window.location = options.url

  // Make it happen.
  // i think im not getting it right.
  $(window).load(
      function() {
          $container.html(data)
      }
  );

喜欢Official way to ask jQuery wait for all images to load before executing something但它是在 jquery ajax 请求之后。

正如您在底部看到的//让它发生,我试图在html(数据)中的所有图像准备好提供服务后返回html(数据),我们能以某种方式做到这一点吗?

谢谢!

亚当·拉马丹

最佳答案

我相信在浏览器开始加载任何图像之前,您必须将加载的 HTML 插入到 DOM 中。您应该能够将其插入到隐藏的 (display:none) div 中,并在该隐藏的 div 上设置 .load() 回调。

This plugin改进了 .load() 事件,以便在图像加载时更一致地触发该事件。

关于jquery - 等待所有图像加载 jquery ajax/jquery pjax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6393606/

相关文章:

javascript - e.preventdefault 提交时触发的提交不起作用

javascript - $.when - done 在所有 promises 被解决之前被调用

javascript - 通过 jquery 为 Opera 实现占位符 =""

javascript - 如何为我的 div 添加时间延迟,使它们像幻灯片一样一个接一个地出现?

asp.net - 动态添加控件和 AJAX

post - 带有 POST 请求的 Yii 2 中的 Pjax

javascript - Ajax/Javascript - 文件下载

javascript - 如何从 Ajax 调用中获取要在 Bootbox 按钮功能中使用的值?

jquery - 在 Rails 中使用 PJax 加载 Doc Ready 上的所有 jQuery 变量

javascript - 使用 vanilla JavaScript 将事件绑定(bind)到动态创建的 HTML 元素 [无 jquery]