使用 jquery 从外部源加载信息后获取 div 的高度和宽度
我见过类似的问题,但没有找到适合我的情况的答案。我使用 jQuery 创建了一个简单的模式弹出插件,运行如下: $('#popup').popupElement("http://www.example.com #somediv");
当单击选择器 #popup 时,它会从另一个网站加载 div。 click 函数如下所示:
$(thisObj).click(function(e){
e.preventDefault();
$("#popupDiv").load(div);
centerPopup();
loadPopup();
});
问题是当它将外部 div 加载到 popupDiv 中时,popupDiv 的高度和宽度均为 0,因此 centerPopup() 无法将其居中。单击 #popup 链接几次后,popupDiv 的尺寸会自行修复。有没有办法确保在调用 centerPopup() 之前将 popupDiv 设置为正确的尺寸?
I've seen questions similar to this but haven't found an answer for my situation. I've created a simple modal popup plugin using jQuery that runs like this:$('#popup').popupElement("http://www.example.com #somediv");
It loads divs from another website when the selector #popup is clicked. The click function looks like this:
$(thisObj).click(function(e){
e.preventDefault();
$("#popupDiv").load(div);
centerPopup();
loadPopup();
});
The problem is when it loads the external div into the popupDiv, the popupDiv's height and width are 0, so centerPopup() can't center it right. After clicking the #popup link a few times the popupDiv's dimensions fix themselves. Is there a way to ensure the popupDiv gets set to the right dimensions before centerPopup() is called?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将对
centerPopup()
和loadPopup()
的调用放在load()
的回调函数中。这将确保在尝试显示弹出窗口之前内容位于 DOM 中,这意味着高度和宽度应该是可访问的。试试这个:Try placing the calls to
centerPopup()
andloadPopup()
in the callback function of theload()
. This will ensure that the content is in the DOM before you try and show the popup, which means that the height and width should be accessible. Try this:您是否尝试过默认隐藏该元素,然后在调用 centerPopup() 之前,显示它,但保持其可见性隐藏,然后检查它的 width() 和 height() 以查看它是否已经有尺寸?
Have you tried hiding the element by default, then before calling centerPopup(), show() it, but keep it's visibility hidden, then check for it's width() and height() to see if it's already got dimensions?