我如何从内部更改iframe的大小? [英] How can i change the size of an iframe from inside?

查看:276
本文介绍了我如何从内部更改iframe的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆