weixin_33736649 2017-03-09 23:20 采纳率: 0%
浏览 43

jQuery .load()回调

I'm trying to load some content with an AJAX call and AFTER the content is loaded, perform another action. In my example, that latter action is simply an alert but in my real-world example I'm attempting to focus on an input field.

$("#box").load("/favicon.png", function(response) {
     alert('do after the load');
});

https://jsfiddle.net/u0cmmn5h/

As you can see in the fiddle, the alert fires BEFORE the favicon is actually loaded. Isn't the function supposed to fire AFTER the content has been loaded?

  • 写回答

2条回答 默认 最新

  • 普通网友 2017-03-09 23:26
    关注

    Here is an example of using load on an image. Not sure what you have the load on a div.

    The load method will be fired when the image src attribute changes and the image is successfully loaded (note if it fails this wont execute).

    .load() jQuery

    The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.

    $(function() {
        $('img').load(function() {
        alert('done after load');
      }).attr('src', '/favicon.ico');
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <div id="box">
      <img />
    </div>

    </div>
    
    评论
  • from.. 2017-03-09 23:38
    关注

    The callback on .load() executes upon an http response, not when the image is rendered client side.

    评论

报告相同问题?

悬赏问题

  • ¥15 存储过程或函数中的结果集类型变量如何使用。
  • ¥80 关于海信电视聚好看安装应用的问题
  • ¥15 vue引入sdk后的回调问题
  • ¥15 求一个智能家居控制的代码
  • ¥15 ad软件 pcb布线pcb规则约束编辑器where the object matpcb布线pcb规则约束编辑器where the object matchs怎么没有+15v只有no net
  • ¥15 虚拟机vmnet8 nat模式可以ping通主机,主机也能ping通虚拟机,但是vmnet8一直未识别怎么解决,其次诊断结果就是默认网关不可用
  • ¥20 求各位能用我能理解的话回答超级简单的一些问题
  • ¥15 yolov5双目识别输出坐标代码报错
  • ¥15 这个代码有什么语法错误
  • ¥15 给予STM32按键中断与串口通信