简体   繁体   English

如何更改iframe中的iframe宽度?

[英]How to change Iframe width inside an iframe?

I have an Iframe inside a webpage that contains a child Iframe . 我在包含子Iframe的网页中有一个Iframe。 I want to change child iframe width using jquery . 我想使用jquery更改子iframe宽度。 Note that inner iframe doesnt have id. 请注意,内部iframe没有ID。 how can i do that ? 我怎样才能做到这一点 ? (both iframes are from my domain) (两个iframe都来自我的域)

Thankyou very much 非常感谢你

<Iframe id="iframeDoc ">
   // some codes
  <Iframe>
   // another page codes

  </Iframe>
</Iframe>

I have tried these codes but no one did work! 我已经尝试过这些代码,但是没有人工作!

$('#iframeDoc > iframe').width("400px");

$('#iframeDoc iframe').width("400px");

$('#iframeDoc').contents().find("iframe").width("400px");

You could use contentWindow to select the nested iframe: 您可以使用contentWindow选择嵌套的iframe:

let pageiFrame = document.getElementById('iframeDoc')
let nestediFrame = pageiFrame.contentWindow.document.getElementBySelector('iframe')

pageiFrame.style.width = '400px'
nestediFrame.style.width = '400px'

If your iframe lies on a different domain - this can be useful. 如果您的iframe位于其他域上,这可能会很有用。

Try to look into Window.postMessage() method. 尝试查看Window.postMessage()方法。 You can use it inside your iframe and window.addEventListener('message', messageHandlerFn) outside. 您可以在iframe和window.addEventListener('message', messageHandlerFn)外部使用它。

But it suitable only in case when you have possibility to modify sources outside iframe. 但这仅在您有可能在iframe之外修改源的情况下才适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM