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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据