javascript - jquery 将所有样式表插入 iframe

标签 javascript jquery iframe wysiwyg

如何将父窗口的所有样式表插入 iframe 的头部(相同域)?

我基于类似问题尝试的代码:

function () {
    var d = frames[0].document;
    var stylesheets = $("link").outerhtml;
    d.open();
    d.write(
    '<html><head>'+
    stylesheets + 
    '<style type="text/css">'+
    '<\/style><\/head><body><\/body><\/html>'
    );
    d.close();
}

显然这在 IE 之外不起作用。提前致谢。

编辑:根据安东尼的回答尝试:

    $("link[type='text/css']").each(function() {
        var stylesheet = $(this).clone();                                    
        $("iframe").contents().find("head").append(stylesheet);
    });

最佳答案

所选答案的一个问题是它使用了 .html(),它只返回该元素的内部 html 内容,而不是元素本身。这是一个可行的解决方案:

$("link[type='text/css']").clone().appendTo($("iframe").contents().find("head"));

关于javascript - jquery 将所有样式表插入 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1490260/

相关文章:

javascript - 了解 React Router 参数

javascript - 嵌入iframe-YouTube视频不循环播放

javascript - 当 iframe 内容的高度改变时自动调整 iframe 高度(同域)

javascript - 通过Chrome Extension内容脚本在iFrame中激活Zurb Foundation的Joyride

javascript - JQuery 在 IE8 中不工作

javascript - 在 React 中选择产品后如何显示结果

javascript - 如何通过单击显示/隐藏下拉菜单?

javascript - 如何在另一个函数中使用在函数中声明的变量?

javascript - 如何确定在php中使用screen.width的变量window.width?

Javascript - 日期验证 IF 需要语句