简体   繁体   English

如何使用 javascript 从父级向 iFrame 添加样式表?

[英]How do I add a stylesheet to an iFrame from the parent, using javascript?

my JSFiddle: http://jsfiddle.net/gCRuk/1/我的 JSFiddle: http://jsfiddle.net/gCRuk/1/

HTML: HTML:

My replays on SC-Replay.com:<br>
<iframe frameborder="0" name="frame" id="frame1" src="http://www.sc-replay.com/embedPlayer/21368-TheLindyHop.html" style="left: 0pt; position: relative; top: 0pt; width: 980px; height: 500px; border: 0px solid #00214E;" n="" vspace="0" marginheight="0" marginwidth="0" hspace="0" allowtransparency="true"></iframe>

Javascript: Javascript:

var ss = document.createElement("link");
ss.type = "text/css";
ss.rel = "stylesheet";
ss.href = "http://www.lprestonsegoiii.com/WordPress/wp-content/themes/arjuna-x/style.css";

var iframe;
if(document.frames)
    iframe = document.frames["frame"];
else
    iframe = window.frames["frame"];
if(document.all)
    iframe.document.createStyleSheet(ss.href);
else
    iframe.document.getElementsByTagName("head")[0].appendChild(ss);

I followed this guide: http://www.geekdaily.net/2007/09/19/javascript-adding-a-stylesheet-to-an-iframe/我遵循了本指南: http://www.geekdaily.net/2007/09/19/javascript-adding-a-stylesheet-to-an-iframe/

And I'm not really sure what is happening here / what is going wrong.而且我不确定这里发生了什么/出了什么问题。

Generally speaking, you can't do that cross-domain due to JavaScript's Same Origin Policy .一般来说,由于 JavaScript 的Same Origin Policy ,您无法进行跨域操作。

It's possible if the iframe page, container page and the CSS file are all served from the same domain, which is what is assumed (but unfortunately not mentioned!) in the tutorial on GeekDaily .如果 iframe 页面、容器页面和 CSS 文件都来自同一个域,这是可能的,这是GeekDaily教程中假设的(但不幸的是没有提到!)。

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

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