jQuery 使用jQuery获取iframe中元素的高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20504086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get height of element inside iframe with jQuery
提问by Giorgio
I have a file containing an "iframe" element
我有一个包含“iframe”元素的文件
<iframe src="test.php" width="100%" id="frameDemo"></iframe>
and a file test.php containing a div of unknown height.
以及一个包含未知高度的 div 的文件 test.php。
<div>some content...</div>
I would like to create a Jquery script to get the height of the div inside the iframe.
I've tried the following but I receive a null response.
我想创建一个 Jquery 脚本来获取 iframe 内 div 的高度。
我尝试了以下操作,但收到空响应。
$(document).ready(function()
{
var mydiv = $( "#frameDemo" ).contents().find("div");
var h = mydiv.height();
alert(h);
});
Can anybody please help me? Thanks in advance.
有人可以帮我吗?提前致谢。
回答by adeneo
Assuming the iframe is on the same domain (otherwise it's not possible), you should wait for the iframe to load first :
假设 iframe 在同一个域中(否则不可能),您应该先等待 iframe 加载:
$(document).ready(function() {
$( "#frameDemo" ).on('load', function() {
var mydiv = $(this).contents().find("div");
var h = mydiv.height();
alert(h);
});
});