当$ .get完全完成时执行某些操作

时间:2011-07-05 17:11:15

标签: javascript jquery html css html5

我有问题。当我的GET完全完成时,我想做点什么,因为我需要将BG放在中间。这个函数完全适用于调整大小,但是当我加载图像onClick时这不起作用。有什么方法可以避免这个吗?

示例:

getBG.php返回<img id="bgImage" src="123.jpg" />

问题:

它无法正确计算变量a,因为我认为它是在图像加载之前尝试这样做的。当图像完全加载时,它可以很好地使用onResize事件。

$.get("getBg.php", {
            img: (this).id
        },
        function(data){
            $("#bg").html(data);
            var a = $("#bgImage").height() - $(window).height();
            $("#bgImage").css("margin-top", - a / 2);
        });

2 个答案:

答案 0 :(得分:1)

您需要直接挂钩新图像的加载事件。 我假设#bgImage的元素是图像,这可能是你想要的。

$.get("getBg.php", {
            img: (this).id
        },
        function(data){
            $("#bg").html(data);
            $("#bgImage").load( function() {
                var a = $(this).height() - $(window).height();
                $(this).css("margin-top", - a / 2);
            }
        });

答案 1 :(得分:0)

您需要使用setTimeout循环,直到$("#bgImage").height()大于零。

相关问题