Iframe从父级继承 [英] Iframe inherit from parent

查看:313
本文介绍了Iframe从父级继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使< iframe> 继承其父级的样式和javascript。

How do I make an <iframe> inherit its parent's styles and javascript.

var parentHead = $("head", parent.document).html();
$("head").html(parentHead);

但是,它除去了< script> 标签。
此外,我没有看到影响我的iframe的样式。

But, it strips out the <script> tags. Moreover, I do not see the styles affecting my iframe.

有没有更好的/任何其他方法,我错过了?
感谢。

Is there a better/any other approach to this that I'm missing? Thanks.

推荐答案

您可以通过在iframe中使用这样的代码继承父代的CSS: / p>

You can "inherit" the CSS of the parent by having such code in the iframe:

<head>
<script type="text/javascript">
window.onload = function() {
    if (parent) {
        var oHead = document.getElementsByTagName("head")[0];
        var arrStyleSheets = parent.document.getElementsByTagName("style");
        for (var i = 0; i < arrStyleSheets.length; i++)
            oHead.appendChild(arrStyleSheets[i].cloneNode(true));
    }
}
</script>
</head>

在IE,Chrome和Firefox中对我来说很好。

Worked fine for me in IE, Chrome and Firefox.

关于JavaScript,
我找不到直接将父JavaScript添加到iframe的方法,但是您可以在任何地方添加 parent。在父级中使用JS,例如:

Regarding JavaScript, I couldn't find a way to add the parent JavaScript into the iframe directly, however you can add parent. anywhere to use the JS from within the parent, for example:

<button type="button" onclick="parent.MyFunc();">Click please</button>

这将调用 MyFunc 点击按钮时的父页面。

This will invoke function called MyFunc defined in the parent page when the button is clicked.

这篇关于Iframe从父级继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆