我有一个iframe,在其中加载一个页面..当在iframe的页面上点击一个链接时,调整父级的主体,也就是拥有iframe主体的文档的大小..
Body <------------------------
Iframe |
Page |
Link... Upon Click, resize body --
我该如何做?我试过Parent.document.body.style.width =“200px”。它不起作用。不知道该怎么办。请帮助!
function Parentresize(id)
{
var newheight = 2000;
var newwidth = 2000;
parent.document.getElementById(id).height= (newheight) + "px";
parent.document.getElementById(id).width= (newwidth) + "px";
}
答案 0 :(得分:1)
我认为您错过了style.height
和style.width
这是我在iframe
<script language="javascript">
function Parentresize(id)
{
alert("U r there");
var newheight = 200;
var newwidth = 200;
parent.document.getElementById(id).style.height = newheight + "px";
parent.document.getElementById(id).style.width = newwidth + "px";
}
</script>
<a href="javascript:Parentresize('sam');" >Call Me </a>
答案 1 :(得分:1)
好的我明白了。这段代码就是这样做的。由于跨浏览器兼容性的额外代码,它有点冗长:
function WindowSize()
{
WinWidth = 0, WinHeight = 0; //Initialize variables with a value of 0;
if(typeof(parent.window.innerWidth) == 'number') //Well I need the parent width of the iFrame aka browser width. Width is a number, not undefined..
{
//FireFox/Opera..
WinWidth = parent.window.innerWidth;
WinHeight = parent.window.innerHeight;
}
else if(parent.document.documentElement && (parent.document.documentElement.clientWidth || parent.document.documentElement.clientHeight))
{
//IE 6+
WinWidth = parent.document.documentElement.clientWidth;
WinHeight = parent.document.documentElement.clientHeight;
}
return WinWidth;
}
function BodyResize(ID)
{
var IDTag = parent.document.getElementById(ID);
if(IDTag.clientWidth == '1024')
IDTag.style.width = (WindowSize() + 'px');
else
IDTag.style.width = '1024px';
}
只需调用此函数BodyResize(ID)
作为
<body id="mainbody" onClick="BodyResize('mainbody')">
或<iframe id="interframe" onClick="BodyResize('interframe')">