日期:2014-05-16  浏览次数:20379 次

想让jquery先获取iframe的宽和高,然后dialog再居中,我的代码问题在哪?
通过一下代码,现在得到的结果是dialog先居中,然后再通过iframe的宽高改变了dialog大小,这样我的dialog就没法居中了,要怎么办才能居中呢?怎样让load()方法执行完后程序再执行下一步?


var iframeBoxHeight = $("#" + idName).height();
var iframeBoxWidth = $("#" + idName).width();
contentBox = "<div id=\"" + idName + "-contentBox\" class=\"green-ui-dialog-contentBox\"></div>";
var contentBoxObj = $(contentBox);
$("<iframe id=\"" + idName + "-iframeBox\" src=\"" + urlStr + "\"></iframe>").appendTo(contentBoxObj);
$this.append($(contentBoxObj));
$("#" + idName + "-iframeBox").load(function() {
iframeBoxHeight = $(this).contents().find("p").height() + 30;
iframeBoxWidth = $(this).contents().find("p").width() + 30;
$("#" + idName + "-iframeBox").css({
"height": iframeBoxHeight + "px",
"width": iframeBoxWidth + "px"
});
});

$this.css({
"display": "none",
"left": ($(window).width() / 2 - iframeBoxWidth / 2) + "px",
"top": ($(window).height() / 2 - iframeBoxHeight / 2) + "px",
"z-index": "10000"
});

------解决方案--------------------
引用:
Quote: 引用:

$this.append($(contentBoxObj));  这个this是指哪个jquery对象?  $(this)


是我的$("#dialog")对象
那应该这么写吧 $(this)
------解决方案--------------------
$this.css({
    "display": "none",
    "left": ($(window).width() / 2 - iframeBoxWidth / 2) + "px",
    "top": ($(window).height() / 2 - iframeBoxHeight / 2) + "px",
    "z-index": "10000"
});
扔到load方法里面去