我有这样的代码来向fancybox iframe中的页面添加样式表。我可以对父页执行同样的操作吗?
我试过了,但没用。。。
window.parent.changeStyles(stylesheet)
;
函数更改样式{$(",{rel:"样式表",type:"text/css",href:样式}).appendTo("头");};
$('.change-styles').on('click',function(){
var stylesheet = $(this).attr('href');
changeStyles(stylesheet);
return false
});
首先,定义一个父文件:
<html>
<head></head>
<body>
<iframe src="iframe.html"></iframe>
</body>
</html>
然后将iframe文件命名为iframe.html
:
<html>
<head>
<script>
function addCSSToParent(url) {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = url;
link.type = 'text/css';
window.parent.document.getElementsByTagName('head')[0].appendChild(link);
}
// Add some CSS
addCSSToParent('some.css');
</script>
</head>
<body>This is the iframe.</body>
</html>
addCSSToParent()
将在具有给定URL的父级的头中添加一个新的DOM元素。请确保在服务器环境中运行此程序,因为访问父级可以工作,但在Chrome中会出现一些安全错误。