我如何从内部更改iframe的大小? [英] How can i change the size of an iframe from inside?
问题描述
我正在用jquery进行开发,我偶然发现了下一个问题:我在主页面中添加了一个IFrame,我想从里面调整它们的大小。我尝试了一些想法,但没有成功。
以下是我的代码:
index.html
< html>
< head>
< title>索引< / title>
< / head>
< body>
< iframe id =myframesrc =frame.htmlwidth =100pxheight =100px>< / frame>
< / body>
< / html>
frame.html
< HTML>
< head>
< title> IFrame< / title>
< script>
document.width = 500;
document.height = 500;
< / script>
< / head>
< body>
< h2> My IFrame< / h2>
< / body>
< / html>
当您创建 IFRAME
浏览器自动为'窗口'中的IFRAME添加一个
'窗口'
对象
您需要更改 IFRAME
的大小,而不是文档的大小。
请尝试以下代码:
对于 JavaScript
:
window.parent.document.getElementById('myframe')。width ='500px';
window.parent.document.getElementById('myframe')。height ='500px';
对于 jQuery
:
$('#myframe',window.parent.document).width('500px');
$('#myframe',window.parent.document).height('500px');
I'm developing with jquery and I stumbled with the next problem: I added an IFrame inside the main page and I want to re size them from inside. I tried some ideas but without success.
Here is my code:
index.html
<html>
<head>
<title>Index</title>
</head>
<body>
<iframe id="myframe" src="frame.html" width="100px" height="100px"></frame>
</body>
</html>
frame.html
<html>
<head>
<title>IFrame</title>
<script>
document.width = 500;
document.height = 500;
</script>
</head>
<body>
<h2>My IFrame</h2>
</body>
</html>
When you create an IFRAME
the browser automatically adds a 'window'
object for the IFRAME inside the 'window'
object of main page.
You need to change the size of the IFRAME
instead of the size of the document.
Try this code:
For JavaScript
:
window.parent.document.getElementById('myframe').width = '500px';
window.parent.document.getElementById('myframe').height = '500px';
And for jQuery
:
$('#myframe', window.parent.document).width('500px');
$('#myframe', window.parent.document).height('500px');
这篇关于我如何从内部更改iframe的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!