【问题标题】:How to load script after ajax load?ajax加载后如何加载脚本?
【发布时间】:2016-05-22 22:55:41
【问题描述】:
var currentTallest = 0,
 currentRowStart = 0,
 rowDivs = new Array(),
 $el,
 topPosition = 0;

$('.blocks').each(function() {

$el = $(this);
topPosition = $el.position().top;

if (currentRowStart != topPosition) {

 // we just came to a new row.  Set all the heights on the completed row
 for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
   rowDivs[currentDiv].height(currentTallest);
 }

 // set the variables for the new row
 rowDivs.length = 0; // empty the array
 currentRowStart = topPosition;
 currentTallest = $el.height();
 rowDivs.push($el);

} else {

 // another div on the current row.  Add it to the list and check if it's taller
 rowDivs.push($el);
 currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);

}

// do the last row
for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
  rowDivs[currentDiv].height(currentTallest);
}

});​

嗨,我正在使用上面的代码为引导程序创建等高的列,它可以工作。但是我也在使用无限滚动库来加载更多内容,并且在我向下滚动后,脚本没有加载到新列中,我知道这是因为 ajax 在脚本加载后加载。我知道 .ajaxComplete() 句柄应该可以完成这项工作,但我还没有找到正确的方法。

【问题讨论】:

    标签: jquery ajax infinite-scroll


    【解决方案1】:

    我不确定我的问题是否正确,但您可能需要检查 $.getScript() 函数并在您的 ajax 调用完成时使用它:

    $.ajax({
    
        // rest of the code
    
        complete: function() {
             $.getScript("path/to/script.js", function() {
                  alert('All done!');
             });
        }
    })
    

    希望这会有所帮助!

    【讨论】:

    • 很好,我从来不知道 .getScript 曝光。我得试试。
    • 如果我要加载的脚本在同一个脚本文件中怎么办?我正在将所有脚本文件编译成一个
    • 在这种情况下,我猜您需要将要在完成回调后执行的代码包装到一个函数中,并在您的 ajax 调用完成时调用该函数。也许你应该创建一个 jsfiddle,这样我就能看到整个代码并帮助你。
    猜你喜欢
    • 1970-01-01
    • 2016-08-12
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 2016-08-27
    • 2019-02-03
    相关资源
    最近更新 更多