jQuery 将 CSS 添加到 iFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6960406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:36:40  来源:igfitidea点击:

Add CSS to iFrame

jquerycssiframe

提问by Peter Kazazes

Possible Duplicate:
How to apply CSS to iFrame?

可能的重复:
如何将 CSS 应用于 iFrame?

Right now I'm loading an iframe via jquery:

现在我正在通过 jquery 加载一个 iframe:

 $(window).load(function(){
  $('iframe').load(function() {
      $('iframe').show()
      $('#loading').hide();

    });
    $('#loading').show();
    $('iframe').attr( "src", "http://www.url.com/");

  });

And I have a custom style sheet that I would like to apply to that page. The page within the iframe is not on the same domain, which is why it's tricky. I've found solutions that allow you to add individual tags, but not entire stylesheets.

我有一个自定义样式表,我想应用于该页面。iframe 中的页面不在同一个域中,这就是为什么它很棘手。我找到了允许您添加单个标签而不是整个样式表的解决方案。

EDIT:The page in question is loaded via file://in Mobile Safari so the Cross-domain policy does not apply.

编辑:有问题的页面是通过file://Mobile Safari加载的,因此跨域策略不适用。

回答by Jacek Kaniuk

Based on solution You've already found How to apply CSS to iframe?:

基于解决方案您已经找到了如何将 CSS 应用到 iframe?

var cssLink = document.createElement("link") 
cssLink.href = "file://path/to/style.css"; 
cssLink .rel = "stylesheet"; 
cssLink .type = "text/css"; 
frames['iframe'].document.body.appendChild(cssLink);

or more jqueryish (from Append a stylesheet to an iframe with jQuery):

或更多 jqueryish(从Append a stylesheet to an iframe with jQuery):

var $head = $("iframe").contents().find("head");                
$head.append($("<link/>", 
    { rel: "stylesheet", href: "file://path/to/style.css", type: "text/css" }));

as for security issues: Disabling same-origin policy in Safari

至于安全问题:在 Safari 中禁用同源策略