简体   繁体   English

如何将外部样式表和javascript导入iframe

[英]how to import external stylesheet and javascript into an iframe

I just want to import an external stylesheet and a js into an iframe's head tag. 我只想将外部样式表和js导入到iframe的head标签中。 Is it possible ? 可能吗 ?

i have tried in the html head a custom jquery function ` 我已经在html头部尝试了一个自定义的jquery函数`

$(this).contents().find('head').append('<link href="http://mywebsite.com/css/iframe.css?" rel="stylesheet" type="text/css" />')`

The window object in the iframe is the property of the page that was loaded into the iframe. iframe中的window对象是加载到iframe中的页面的属性。 THE ONLY WAY for the containing page to manipulate the iframe's window object is if the domain of the containing page and the iframe page are the same. 包含页面操作iframe窗口对象的唯一方法是包含页面的域和iframe页面是否相同。 (due to the cross-domain restriction policy) (由于跨域限制政策)

I don't believe it is possible to include a stylesheet within an iframe using JavaScript as the frame will have already rendered before the stylesheet was added. 我不相信可以使用JavaScript在iframe中包含样式表,因为在添加样式表之前框架已经渲染。

You can alter the styles of elements in the iframe's DOM using .find().css() . 您可以使用.find().css()更改iframe DOM中元素的样式。 (I have done something similar for building a theme generator for a site with an iFrame preview pane). (我已经为使用iFrame预览窗格为网站构建主题生成器做了类似的事情)。

You should be wary of changing the CSS of an iFrame if it comes from someone else's website as they may change their own CSS or markup at any time which could mess up what you are trying to achieve. 你应该警惕改变iFrame的CSS,如果它来自别人的网站,因为他们可能随时改变他们自己的CSS或标记,这可能搞乱你想要实现的目标。

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

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