How can i change the size of an iframe from inside?

前端 未结 3 1119
-上瘾入骨i
-上瘾入骨i 2020-12-25 12:20

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 witho

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-25 12:58

    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');
    

提交回复
热议问题