IE不等待从ajax调用加载内容

时间:2013-10-22 15:27:34

标签: jquery internet-explorer pageload

我正在尝试将jquery滚动条应用于使用另一个本地jquery调用的内容填充的div。我的问题是IE在调用滚动条的脚本之前似乎没有等待div中的内容加载...因此,滚动条无法正常工作。

  • 所有其他浏览器都运行良好,IE(版本8,9)是唯一一个无效的浏览器。
  • 当div包含静态html代码时,IE工作正常
  • 对scroll函数的调用在window.load

奇怪的是:如果我在调用滚动代码之前粘贴了alert()调用,它就会起作用。

我尝试过添加一个setTimeout()函数,但是没有用。

代码看起来像这样

<head>
  <script>
      $(document).ready(function() {
          // ajax call to populate scrollable div with html content
       });
  </script>
</head>
<body>

    <div class="scrollableDiv">

       // content populated by script call above

    </div>

    <...... lots more html here .....>
    <!-- very bottom of page -->
    <script>
       $(window).load(function(){
          // alert ("IF I ADD THIS ALERT, EVERYTHING WORKS FINE IN IE");
         $('.scrollableDiv').ClassyScroll();

       });
    </script>
  </body>

任何想法???

2 个答案:

答案 0 :(得分:1)

$(document).ready(function() {

文档准备就绪时,您的div内容已填充。

$(window).load(function(){

只要您的网页完全加载,就会调用ClassyScroll函数。

我们需要知道你正在做什么来填充你的div,但它可能是异步的,并且不会阻止加载事件。

无论如何,将它们用于同一功能通常是一个坏主意。我建议的是,只要你知道你的div被填充就会简单地调用ClassyScroll(如果是同步的,则在回调之后,如果是异步则在回调之后)。

类似的东西:

$(document).ready(function() {
    // call function to populate scrollable div with html content
    populateDivWhatever();
    // call your classyscroll function
    $('.scrollableDiv').ClassyScroll();
});

答案 1 :(得分:0)

Jquery Ajax will help

看来你正在通过ajax调用加载scrollableDiv的内容吗?如果是这样,那么当window.load发生时,该数据可能不存在。 window.load不会等待异步调用完成。

解决方法是放置$('。scrollableDiv')。ClassyScroll();在$(document).ready(function(){})的末尾;或者把它放在ajax调用的成功回调中。

我在jquery中一直做ajax事情,它看起来像这样:

$.ajax({
    beforeSend: function(req, settings) {
        // display a please wait spinner
    },
    complete: function(req, status) {
        // hide the please wait spinner
        // also a possible place to put code you want run after the DOM has changed
    },
    async: true,  // could omit as this is the default
    data: {param1:foo, param2:bar},
    dataType: "html",
    error: function(req, status, err) {
        // report the bad error
    },
    success: function(data, status, req) {
              // do the fun stuff here
    },
    type: "POST",
    url: someURL
});