擷取到 Iframe 架構的實際高度,去掉 Iframe 中的捲軸,iframe捲軸

來源:互聯網
上載者:User

擷取到 Iframe 架構的實際高度,去掉 Iframe 中的捲軸,iframe捲軸

在介紹中,本文使用了 Jquery ,並且注意,父頁面和子頁面中都含有 DOCTYPE 

在父頁面中擷取  iframe 內容 的實際高度

var h = $("#test_frame").get(0).contentWindow.document.body.scrollHeight;

在父頁面中更改 Iframe 的高度,使 Iframe 沒有捲軸

$("#test_frame").attr("height",h+100);


例子:

父頁面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>
<script src="jquery-1.2.6.js" type="text/javascript" ></script>
<script>
function test(){
//擷取到 iframe 的高度:
//alert("parent:"+$("#test_frame").get(0).contentWindow.document.body.scrollHeight);
//賦值高度
var h = $("#test_frame").get(0).contentWindow.document.body.scrollHeight;
$("#test_frame").attr("height",h+100)
}
</script>
<body style="text-align:center">
這是父頁面<br />
<iframe id="test_frame" src="test25.html" frameborder="1" scrolling="no" ></iframe>
    <input type="button" value="測試" onclick="test()" />
</body>
</html>


子頁面:test25.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>
<body style="overflow:hidden">
this is a test
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    aaa
</body>
</html>
<script>
//在 頁面中擷取本頁面的實際高度
alert(window.document.body.clientHeight);
</script>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.