<script>
var href;
$(function(){
$("a.load").click(function (e) {
e.preventDefault();
href = this;
// cover all bookmarks
$("."+($(this).attr("class"))).css('border-bottom', 'solid 1px black');
$("."+($(this).attr("class"))).css('background-color', '#F5F5F5');
// uncover chosen bookmark
$("#"+($(this).attr("id"))).css('border-bottom', 'solid 2px white');
$("#"+($(this).attr("id"))).css('background-color', 'white');
$("#tempMain").load($(this).attr("href")); // load content to temp div
setTimeout(function() {resizeDivs();}, 500); // synchronize size of #main and #rightColumn
});
});
function resizeDivs() {
var heightNew = $("#tempMain").css("height");
$("#rightColumn").css('height',heightNew);
$("#main").css('height',heightNew);
// $('#main').load($(href).attr('href'));
$("#main").html($("#tempMain").html()); // is faster than loading again
}
</script>
我正在通过jQuery函数将我的主页的选定div加载到子页面。为了同步主div和右列的高度,我使用jQuery的.css()
方法。我希望调整大小看起来很顺利,所以我已将它解决为以下步骤:
1.将子页面的内容加载到不可见的临时div。
2.计算该临时分区的高度。
3.将主div和右列的高度更改为该temp div的高度。
4.将子页面的内容加载到主div。
如何在调用另一个函数之前等待div加载?
但是,我知道,我现在这样做的方式是因为使用setTimeout()
作为等待要加载的内容即兴的漂亮跛脚 - 我用$(document).ready
尝试,但没有运气。使用全局变量(var href
)也似乎并不普遍,但通过this
运算符$("a.load").click
函数apply()
也没有工作。
如何做到“正确”?
可能重复[如何要等到一个成分存在?(http://stackoverflow.com/questions/5525071/how-to-wait-until-an-element-exists) – user2284570