本文介绍了将CSS添加到iFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
右现在我通过jquery加载一个iframe:
Right now I'm loading an iframe via jquery:
$(window).load(function(){
$('iframe').load(function() {
$('iframe').show()
$('#loading').hide();
});
$('#loading').show();
$('iframe').attr( "src", "http://www.url.com/");
});
我有一个自定义样式表,我想申请该页面。 iframe中的网页不在同一个网域,这就是为什么它很棘手。我找到了允许您添加单个标签,但不能添加整个样式表的解决方案。
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.
EDIT: code> file:// ,因此跨域策略不适用。
The page in question is loaded via file://
in Mobile Safari so the Cross-domain policy does not apply.
推荐答案
基于解决方案您已找到:
Based on solution You've already found How to apply CSS to 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);
或更多jqueryish(从):
or more jqueryish (from 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" }));
:
这篇关于将CSS添加到iFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!