简体   繁体   English

如何在该页面的iframe中通过javascript控制页面?

[英]How can i control page throught javascript inside an iframe of that page?

I have an iframe with Js code to run, and i wan't to control the iframe DOM options and css style. 我有一个要运行Js代码的iframe,并且我不想控制iframe DOM选项和CSS样式。
How can i do it? 我该怎么做? If you wan't to use jQuery, thats ok, but i preffer pure javascript... 如果您不想使用jQuery,那没关系,但是我推荐使用纯JavaScript ...

Things that i want to modify: 我要修改的内容:
- frameborder - 框架边框
- css styles (float, display, position, overflow) -CSS样式(浮动,显示,位置,溢出)
- no scrolling bar -没有滚动条

EDIT : The code run's inside the iframe 编辑 :代码运行 iframe中

Thanks 谢谢

I assume from your previous questions that the document inside the iframe is not on the same domain as the outlying page. 从您先前的问题中,我认为iframe中的文档与外围页面不在同一个域中。

In that case, your options are extremely limited, again because of the Same Origin Policy . 在这种情况下,同样由于同源政策 ,您的选择极为有限。 You will not have Javascript access to the elements or CSS of the outlying page. 您将无法使用Javascript访问外围页面的元素或CSS。 That includes the <iframe> element on that page. 这包括该页面上的<iframe>元素。

Any design decisions you will therefore have to put inline into the iframe tag: 因此,您必须将所有设计决策内联到iframe标签中:

<iframe frameborder="0" style="float: left; width: 500px; height: 200px ....">

I think that applies to everything you mention except the scroll bar behaviour: That you can control inside the iframe document using the body element's overflow property. 我认为这适用于您提到的所有内容,除了滚动条行为:您可以使用body元素的overflow属性在iframe文档中进行控制。

The only alternative idea that comes to mind is have the sites that embed your iframe also include a style sheet that comes from your domain in the head section of the document. 想到的唯一替代想法是,在嵌入iframe的网站上还包含来自您域的样式表,该样式表来自文档的head部分。 That CSS you would, of course, fully control. 您可以完全控制该CSS。

If you limit the design rules in that CSS to your iframe (eg by using the #id selector), that would work as well. 如果您将CSS中的设计规则限制为您的iframe(例如,通过使用#id选择器),那也可以正常工作。 The downside is that if the style sheet does not get included, your iframe will break. 缺点是,如果包含样式表,则iframe会损坏。 This is why most providers of widgets use inline CSS. 这就是为什么大多数窗口小部件提供程序都使用嵌入式CSS的原因。

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

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