如何在 ajax 加载后加载脚本?
How to load script after ajax load?
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);
}
});
您好,我使用上面的代码为 bootstrap 创建了等高的列,并且有效。但我还使用无限滚动库来加载更多内容,在我向下滚动后脚本不会加载到新列中,我知道这是因为 ajax 在脚本加载后加载。我知道 .ajaxComplete() 句柄应该可以完成这项工作,但我还没有找到正确的方法。
我不确定我是否答对了问题,但您可能需要检查 $.getScript()
函数并在 ajax 调用完成后使用它:
$.ajax({
// rest of the code
complete: function() {
$.getScript("path/to/script.js", function() {
alert('All done!');
});
}
})
希望这对您有所帮助!
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);
}
});
您好,我使用上面的代码为 bootstrap 创建了等高的列,并且有效。但我还使用无限滚动库来加载更多内容,在我向下滚动后脚本不会加载到新列中,我知道这是因为 ajax 在脚本加载后加载。我知道 .ajaxComplete() 句柄应该可以完成这项工作,但我还没有找到正确的方法。
我不确定我是否答对了问题,但您可能需要检查 $.getScript()
函数并在 ajax 调用完成后使用它:
$.ajax({
// rest of the code
complete: function() {
$.getScript("path/to/script.js", function() {
alert('All done!');
});
}
})
希望这对您有所帮助!