1

我正在尝试WebView使用 iframe 在一个 html 中显示多个 html 文件

我在 html 文件中使用以下脚本来调整当前 html 中 iframe 的高度。

<body onload="resizeFrames()" link="#bb7722" vlink="#bb7722" alink="#bb7722" > 


    <script type="text/javascript">
        // Firefox worked fine. Internet Explorer shows scrollbar because of frameborder
        function resizeFrames() {
            var all_IFrames = document.getElementsByTagName('iframe');
            i = 0;
            while (frame = all_IFrames.item(i++)) {
                resizeFrame(frame);
            }            
        }
        function resizeFrame(f) {

            f.style.height = f.contentWindow.document.body.scrollHeight + "px";
        }
    </script>

    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="b.html"></iframe>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="c.html"></iframe>

为了使 javascript 工作,我使用以下代码:

WebSettings webSettings = wb.getSettings();
webSettings.setJavaScriptEnabled(true);

我像这样加载html文件:

webView.loadUrl("file:///android_asset/a.html");

问题是调整后的 iframe 的大小有时太大而有时太小。

我在带有 android 2.2 的 Galaxy s 上对其进行了测试,所有帧都更小(最后被切断),而在其他带有 android 4.0.4 的设备中,所有帧都更大(添加空白空间)

如果我删除了webSettings.setJavaScriptEnabled(true);Galaxy s 中的问题消失了,但在第二个设备中,框架根本不会调整大小

完整的html代码:

<!DOCTYPE html>
<html>
<head>  
    <style>


    body{
    color: #aa6611;
    font-size:20px;
    }

    .link {
    font-weight: bold;
    color: #bb7722;
    }
    </style>


</head>

<body onload="resizeFrames()" link="#bb7722" vlink="#bb7722" alink="#bb7722" > 


    <script type="text/javascript">
        // Firefox worked fine. Internet Explorer shows scrollbar because of frameborder
        function resizeFrames() {
            var all_IFrames = document.getElementsByTagName('iframe');
            i = 0;
            while (frame = all_IFrames.item(i++)) {
                resizeFrame(frame);
            }           
        }
        function resizeFrame(f) {
            f.style.height = f.contentWindow.document.body.scrollHeight + "px";
        }
    </script>

    <a name="top"></a>


    <font size = "6" color="#ffaa55">Guide</font>
    <br /> 
    <br />

    <a href="#mainscreen" class="link" >Main screen</a> <br />
    <a href="#inventory" class="link">Inventory</a> <br />
    <a href="#store" class="link">Store</a> <br />
    <a href="#market" class="link">Market</a> <br />
    <a href="#creatureattackdialog" class="link">Creature Attack Dialog</a> <br />
    <a href="#userattackdialog" class="link">User Attack Dialog</a> <br />
    <a href="#miniattackdialog" class="link">Mini Attack Dialog</a> <br />
    <a href="#profile" class="link">Profile</a> <br />
    <a href="#creatureinfo" class="link">Creature info</a> <br />
    <a href="#userinfo" class="link">User info</a> <br />
    <a href="#iteminfo" class="link">Item info</a> <br />
    <a href="#lastattackers" class="link">Last Attackers</a> <br />
    <a href="#achievementshistory" class="link">Achievements History</a> <br />
    <a href="#achievementsDialog" class="link">Achievement info</a> <br />
    <a href="#options" class="link">Options</a> <br />


    <br /> 

    <p>
    <a name="mainscreen"></a>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="mainscreen.html"></iframe>
    </p>
    <a href="#top" class="link">Back</a> <br />

    <p>
    <a name="inventory"></a>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="inventory.html"></iframe>
    </p>
    <a href="#top" class="link">Back</a> <br />

    <p>
    <a name="store"></a>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="store.html"></iframe>
    </p>
    <a href="#top" class="link">Back</a> <br />

    <p>
    <a name="market"></a>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="market.html"></iframe>
    </p>
    <a href="#top" class="link">Back</a> <br />

    <p>
    <a name="creatureattackdialog"></a>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="attackDialogCreature.html"></iframe>
    </p>
    <a href="#top" class="link">Back</a> <br />

    <p>
    <a name="userattackdialog"></a>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="attackDialogUser.html"></iframe>
    </p>
    <a href="#top" class="link">Back</a> <br />

    <p>
    <a name="miniattackdialog"></a>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="miniattackdialog.html"></iframe>
    </p>
    <a href="#top" class="link">Back</a> <br />

    <p>
    <a name="profile"></a>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="profile.html"></iframe>
    </p>
    <a href="#top" class="link">Back</a> <br />

    <p>
    <a name="creatureinfo"></a>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="creatureInfo.html"></iframe>
    </p>
    <a href="#top" class="link">Back</a> <br />

    <p>
    <a name="userinfo"></a>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="userInfo.html"></iframe>
    </p>
    <a href="#top" class="link">Back</a> <br />

    <p>
    <a name="iteminfo"></a>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="itemInfo.html"></iframe>
    </p>
    <a href="#top" class="link">Back</a> <br />

    <p>
    <a name="lastattackers"></a>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="lastattackers.html"></iframe>
    </p>
    <a href="#top" class="link">Back</a> <br />

    <p>
    <a name="achievementshistory"></a>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="acheivementhistory.html"></iframe>
    </p>
    <a href="#top" class="link">Back</a> <br />

    <p>
    <a name="achievementsDialog"></a>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="acheivementDialog.html"></iframe>
    </p>
    <a href="#top" class="link">Back</a> <br />

    <p>
    <a name="options"></a>
    <iframe width="100%" scrolling="no" frameborder=0 border=0 src="optionsDialog.html"></iframe>
    </p>
    <a href="#top" class="link">Back</a> <br />



</body>
</html>
4

1 回答 1

1

您需要为 iframe 标记中的 onload 处理程序设置侦听器,每个侦听器一个,并在加载 iframe 而不是正文时调整每个侦听器的大小。

var iframe = document.getElementById('yourIF');
var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
iframe.style.height = (iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight) + "px";
于 2012-12-27T12:23:34.430 回答