2011-06-02 47 views
0

我的jsfiddle:http://jsfiddle.net/gCRuk/1/如何使用javascript将样式表从父级添加到iFrame?

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:

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

我遵循这个指南:http://www.geekdaily.net/2007/09/19/javascript-adding-a-stylesheet-to-an-iframe/

而且我真的不知道这里发生了什么/出了什么问题。

回答

4

一般来说,由于JavaScript的Same Origin Policy,你不能做跨域。

如果iframe页面,容器页面和CSS文件都是从同一个域提供的,这是在GeekDaily的教程中假设的(但不幸的是未提及!)。

相关问题