想让jquery先获取iframe的宽和高,然后dialog再居中,我的代码问题在哪?

bianweiall 2013-12-05 11:43:05
通过一下代码,现在得到的结果是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"
});
...全文
237 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
allali 2013-12-10
  • 打赏
  • 举报
回复
$this.css({ "display": "none", "left": ($(window).width() / 2 - iframeBoxWidth / 2) + "px", "top": ($(window).height() / 2 - iframeBoxHeight / 2) + "px", "z-index": "10000" }); 扔到load方法里面去
bianweiall 2013-12-06
  • 打赏
  • 举报
回复
引用 3 楼 u012463264 的回复:
[quote=引用 2 楼 bianweiall 的回复:] [quote=引用 1 楼 u012463264 的回复:] $this.append($(contentBoxObj)); 这个this是指哪个jquery对象? $(this)
是我的$("#dialog")对象[/quote]那应该这么写吧 $(this)[/quote] 是的,代码没贴全,差这个 var $this = $(this);
bianweiall 2013-12-06
  • 打赏
  • 举报
回复
引用 5 楼 u012463264 的回复:
[quote=引用 4 楼 u012463264 的回复:] 这个东西只要把iframe放到div中设置 iframe的高度和宽度自己会居中的。给你个例子 function closeDialog(){ if ($("#div_cmmbDialog").get(0) != null) {$("#div_cmmbDialog").dialog("close");} } function closeConfirmDialog(){ if ($("#div_confirmDialog").get(0) != null) {$("#div_confirmDialog").dialog("close");} } (function($) { //弹出窗口:可包含子窗口 $.showDialog = function(_title, _url, _width, _height){ var iwidth = 600; var iheight = 400; if (_width) iwidth = _width; if (_height) iheight = _height; if ($("#div_cmmbDialog").get(0) == null) { $("body").append("<div id=\"div_cmmbDialog\" title=\""+_title+"\"></div>"); $("#div_cmmbDialog").append("<iframe id='frm_cmmbDialog' name='frm_cmmbDialog' width='100%' height='100%'" +" marginwidth='0' marginheight='0' scrolling='auto' frameborder='0' " +" border='0' framespacing='2' noresize='noresize' vspale='0'></iframe>"); $("#div_cmmbDialog:ui-dialog").dialog("destroy"); $("#div_cmmbDialog").dialog({ autoOpen: false, modal: true, width: iwidth, height: iheight }); }
if (_url.indexOf("?")>0){ _url += "&"+Math.floor(Math.random() * 1000); } else{ _url += "?"+Math.floor(Math.random() * 1000); } $("#ui-dialog-title-div_cmmbDialog").html(_title); $("#frm_cmmbDialog").attr("src",_url); $("#div_cmmbDialog").attr("title",_title); $("#div_cmmbDialog").dialog("open"); }[/quote] 我和你使用的情况不一样,我是通过load()获取iframe src="content.html"的内容的宽和高,因为load()方法是异步的,搞的现在还没load完的时候我的dialog就已经居中了,到load完后在直接撑开了dialog的尺寸,这样就没有居中。 有没有方法可以让load方法异步加载完成后再去执行下面的内容?
别闹腰不好 2013-12-06
  • 打赏
  • 举报
回复
引用 4 楼 u012463264 的回复:
这个东西只要把iframe放到div中设置 iframe的高度和宽度自己会居中的。给你个例子 function closeDialog(){ if ($("#div_cmmbDialog").get(0) != null) {$("#div_cmmbDialog").dialog("close");} } function closeConfirmDialog(){ if ($("#div_confirmDialog").get(0) != null) {$("#div_confirmDialog").dialog("close");} } (function($) { //弹出窗口:可包含子窗口 $.showDialog = function(_title, _url, _width, _height){ var iwidth = 600; var iheight = 400; if (_width) iwidth = _width; if (_height) iheight = _height; if ($("#div_cmmbDialog").get(0) == null) { $("body").append("<div id=\"div_cmmbDialog\" title=\""+_title+"\"></div>"); $("#div_cmmbDialog").append("<iframe id='frm_cmmbDialog' name='frm_cmmbDialog' width='100%' height='100%'" +" marginwidth='0' marginheight='0' scrolling='auto' frameborder='0' " +" border='0' framespacing='2' noresize='noresize' vspale='0'></iframe>"); $("#div_cmmbDialog:ui-dialog").dialog("destroy"); $("#div_cmmbDialog").dialog({ autoOpen: false, modal: true, width: iwidth, height: iheight }); }
if (_url.indexOf("?")>0){ _url += "&"+Math.floor(Math.random() * 1000); } else{ _url += "?"+Math.floor(Math.random() * 1000); } $("#ui-dialog-title-div_cmmbDialog").html(_title); $("#frm_cmmbDialog").attr("src",_url); $("#div_cmmbDialog").attr("title",_title); $("#div_cmmbDialog").dialog("open"); }
别闹腰不好 2013-12-06
  • 打赏
  • 举报
回复
这个东西只要把iframe放到div中设置 iframe的高度和宽度自己会居中的。给你个例子 function closeDialog(){ if ($("#div_cmmbDialog").get(0) != null) {$("#div_cmmbDialog").dialog("close");} } function closeConfirmDialog(){ if ($("#div_confirmDialog").get(0) != null) {$("#div_confirmDialog").dialog("close");} } (function($) { //弹出窗口:可包含子窗口 $.showDialog = function(_title, _url, _width, _height){ var iwidth = 600; var iheight = 400; if (_width) iwidth = _width; if (_height) iheight = _height; if ($("#div_cmmbDialog").get(0) == null) { $("body").append("<div id=\"div_cmmbDialog\" title=\""+_title+"\"></div>"); $("#div_cmmbDialog").append("<iframe id='frm_cmmbDialog' name='frm_cmmbDialog' width='100%' height='100%'" +" marginwidth='0' marginheight='0' scrolling='auto' frameborder='0' " +" border='0' framespacing='2' noresize='noresize' vspale='0'></iframe>"); $("#div_cmmbDialog:ui-dialog").dialog("destroy"); $("#div_cmmbDialog").dialog({ autoOpen: false, modal: true, width: iwidth, height: iheight }); }
别闹腰不好 2013-12-06
  • 打赏
  • 举报
回复
引用 2 楼 bianweiall 的回复:
[quote=引用 1 楼 u012463264 的回复:] $this.append($(contentBoxObj)); 这个this是指哪个jquery对象? $(this)
是我的$("#dialog")对象[/quote]那应该这么写吧 $(this)
bianweiall 2013-12-06
  • 打赏
  • 举报
回复
引用 1 楼 u012463264 的回复:
$this.append($(contentBoxObj)); 这个this是指哪个jquery对象? $(this)
是我的$("#dialog")对象
别闹腰不好 2013-12-06
  • 打赏
  • 举报
回复
$this.append($(contentBoxObj)); 这个this是指哪个jquery对象? $(this)

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧