【问题标题】:jQuery reports incorrect element height in Firefox iframejQuery 在 Firefox iframe 中报告不正确的元素高度
【发布时间】:2010-11-16 06:38:18
【问题描述】:

这里有一个简短的测试来证明我的问题。我有一个加载 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:

<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。这是错误的。

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

【问题讨论】:

    标签: javascript jquery css iframe height


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2018-05-30
      • 2011-06-28
      • 2012-03-03
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 2015-02-22
      • 1970-01-01
      相关资源
      最近更新 更多