使用jQuery获取iframe中元素的高度 [英] Get height of element inside iframe with jQuery

查看:697
本文介绍了使用jQuery获取iframe中元素的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含iframe元素的文件

I have a file containing an "iframe" element

<iframe src="test.php" width="100%" id="frameDemo"></iframe>

以及包含未知高度div的文件test.php。

and a file test.php containing a div of unknown height.

<div>some content...</div>

我想创建一个Jquery脚本来获取iframe中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.

$(document).ready(function() 
{
    var mydiv = $( "#frameDemo" ).contents().find("div");
    var h = mydiv.height();
    alert(h);
});

有人可以帮帮我吗?在此先感谢。

Can anybody please help me? Thanks in advance.

推荐答案

假设iframe位于同一个域(否则无法实现),您应该等待iframe首先加载:

Assuming the iframe is on the same domain (otherwise it's not possible), you should wait for the iframe to load first :

$(document).ready(function() {
    $( "#frameDemo" ).on('load', function() { 
        var mydiv = $(this).contents().find("div");
        var h     = mydiv.height();
        alert(h);
    });
});

这篇关于使用jQuery获取iframe中元素的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆