简体   繁体   English

Javascript从iframe内部更改iFrame的内容

[英]Javascript changing contents of an iFrame, from the inside of the iframe

How to change the innerHTML of an iframe, from the inside of that iframe? 如何从iframe的内部更改iframe的innerHTML?

<iframe>
foo

<script>
.... will change "foo" to "bar" .....
</script>

</iframe>

That's not how iframes work. 这不是iframe的工作方式。

The innerHTML of the iframe tag is fallback content, to be displayed only when the browser does not support iframes. iframe标记的innerHTML是后备内容,仅在浏览器不支持iframe时显示。 All current browsers (including screenreaders) do support them; 当前所有的浏览器(包括屏幕阅读器)都支持它们。 unless your site needs to support truly ancient browsers (we're talking stuff from the mid-1990s, IE5 and below) this can generally be omitted, because it won't be shown to anyone. 除非您的网站需要支持真正的古老浏览器(我们所谈论的是1990年代中期的IE5及更低版本),否则通常可以将其省略,因为不会向任何人显示。

Instead, use the src attribute on the iframe tag to point to a separate web page, which will act separately from the parent window. 相反,请使用iframe标记上的src属性指向单独的网页,该网页将与父窗口分开工作。

<iframe src="this_page_will_be_shown_in_the_frame.html">
  This is fallback content that will not be shown in any current browsers
</iframe>

That framed page can have its own javascript and CSS which can modify its contents independently of the parent window -- so you'd modify the framed page from within the frame exactly the same way you'd modify it if it weren't inside a frame at all. 该框架页面可以拥有自己的javascript和CSS,它们可以独立于父窗口修改其内容-因此,如果您不在框架内部,则可以按照与修改框架完全相同的方式在框架内修改框架页面框架。

If the parent and the framed documents are on the same domain name, you can make the javascript from one affect the other -- by using window.parent from the frame or .contentWindow on the iframe node from the parent -- but by default both the CSS and javascript for each document will work separately and not affect each other. 如果父级文档和框架文档位于同一个域名上,则可以使javascript相互影响-通过使用frame中的window.parent或父级iframe节点上的.contentWindow但默认情况下两者每个文档的CSS和javascript都将分别工作,并且不会互相影响。

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

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