jQuery在Firefox iframe中报告不正确的元素高度 [英] jQuery reports incorrect element height in Firefox iframe

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

问题描述

这里是一个简短的测试来演示我的问题。我有一个加载iframe的页面:

Here a short test to demonstrate my problem. I have a page that loads an iframe:

<html>
    <head>
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
    </head>
    <body>
        <iframe id="iframe" src="box.html" style="width: 100px; height: 100px"></iframe>
        <script>
            $('#iframe').bind('load', function () {
                var div = $(this).contents().find('div');
                alert(div.height());
                alert(div.innerHeight());
                alert(div.outerHeight());
                alert(div.outerHeight(true));
            });
        </script>
    </body>
</html>

iframe(box.html)包含一个样式div:

The iframe (box.html) contains a single styled div:

<html>
    <head>
        <title></title>
        <style>
            div {
                height: 50px;
                width: 50px;
                margin: 5px;
                padding: 5px;
                border: 2px solid #00f;
                background-color: #f00;
            }
        </style>
    </head>
    <body>
        <div></div>
    </body>
</html>

四个警报应分别返回50,60,64和74。这在Safari和Chrome中的预期工作。在FF 3.5.1中,它们都返回64.这是错误的。

The four alerts should return 50, 60, 64 and 74, respectively. This works as expected in Safari and Chrome. In FF 3.5.1, they all return 64. This is wrong.

有谁知道如何强制FF / jQuery返回正确的值?

Does anyone know how I can force FF/jQuery to return the correct values?

推荐答案

请尝试:

var div = $(this).contents().find("html");

这篇关于jQuery在Firefox iframe中报告不正确的元素高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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