使用jQuery我创建了一个弹出对话框,然后填充div。.side-article-title,.side-article-subtitle和.side-article-detail,并使用.load从文章URL中提取信息( )。
但是.side-article-detail的高度需要根据上面提到的其他2的高度而改变,我似乎无法获得变量detailheight来从.height()的ajax返回值中减去值; < / p>
的jQuery
$('body').prepend('<div class="side-article"><div class="side-article-close">X</div><div class="like">Like This</div><div class="article-inner"><div class="side-article-title"></div><div class="side-article-subtitle"></div><div class="side-article-detail"></div></div></div>');
var titleheight = null;
var subtitleheight = null;
var articleHeight = null;
$('.side-article-title').load(thisURL+' #item-title',function(){
titleheight = $('.side-article-title').height();
console.log(titleheight);
});
$('.side-article-subtitle').load(thisURL+' #item-subtitle',function(){
subtitleheight = $('.side-article-subtitle').height();
console.log(subtitleheight);
});
$('.side-article-detail').load(thisURL+' #item-detail',function(){
$('#item-detail').jScrollPane();
});
将高度设置为
的元素 var detailheight = articleHeight - titleheight - subtitleheight;
$('.side-article-detail, .jspContainer').css('height',detailheight);
答案 0 :(得分:1)
ajax的所有功能都需要在回调完成后完成。你不能依赖他们以任何顺序工作:
$.when($('.side-article-title').load(thisURL+' #item-title', function () {...}),
$("...").load(...),
$("...").load(...)
).done(function () {
var detailheight = articleHeight - titleheight - subtitleheight;
});
.done
回调只会在所有的ajax请求完成时触发。