gpt4 book ai didi

javascript - 如何根据其内容的大小将 iFrame 设为 'grow'?

转载 作者:搜寻专家 更新时间:2023-11-01 05:26:51 26 4
gpt4 key购买 nike

我正在动态加载 iFrame,有些页面比其他页面“高”。我希望 iFrame 相应地增长。是否可以?如果是,怎么办?

最佳答案

是的,通过 jquery 是可能的。父页面代码:

<iframe id='ifrm' />

iframe 页面上的脚本:

function alertSize() {
var myHeight = 0;
if (typeof (parent.window.innerWidth) == 'number') {
//Non-IE
myHeight = parent.window.innerHeight;
} else if (parent.document.documentElement
&& (parent.document.documentElement.clientWidth || parent.document.documentElement.clientHeight)) {
//IE 6+ in 'standards compliant mode'
myHeight = parent.document.documentElement.clientHeight;
} else if (parent.document.body && (parent.document.body.clientWidth || parent.document.body.clientHeight)) {
//IE 4 compatible
myHeight = parent.document.body.clientHeight;
}
//window.alert( 'Height = ' + myHeight );
return myHeight;
}

function AssignFrameHeight() {
var theFrame = $("#ifrm", parent.document.body);
var frameHeight1 = getIframeHeight('ifrm');
var frameHeight2 = $(document.body).height();
if ($(document.body)[0]) {
if ($(document.body)[0].bottomMargin)
frameHeight2 += Number($(document.body)[0].bottomMargin);
if ($(document.body)[0].topMargin)
frameHeight2 += Number($(document.body)[0].topMargin);
}
if (frameHeight1 > frameHeight2) {
theFrame.height(frameHeight1 - 20);
} else {
if ($.browser.msie)
theFrame.height(frameHeight2);
else
theFrame.height(frameHeight2 + 50);
}
}

function getIframeHeight(iframeName) {
//var iframeWin = window.frames[iframeName];
var iframeEl = parent.document.getElementById
? parent.document.getElementById(iframeName)
: parent.document.all
? parent.document.all[iframeName]
: null;
if (iframeEl) {
iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous
//var docHt = getDocHeight(iframeWin.document);
// need to add to height to be sure it will all show
var h = alertSize();
//var new_h = (h - 148);
//iframeEl.style.height = h + "px";
return h;
//alertSize();
}
}

回发后重新分配高度:

function pageLoad() { // MS AJAX - UpdatePanel
AssignFrameHeight();
}

$(document).ready(function() { // jQuery
AssignFrameHeight();
});

关于javascript - 如何根据其内容的大小将 iFrame 设为 'grow'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2872967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com