首页 > 解决方案 > javascript中子窗口如何从父窗口继承css样式?

问题描述

我编写了一个函数来创建一个新窗口(子窗口)。

        openReportsWindow:function(content){
        window.name = "parentWin";
        var h = screen.height;
        var w = screen.width;
        if(window.childWrep == null){
            var childWrep = window.open("", 'repWin','left=0,top=0,resizable=yes,status=yes,width='+w +',height=' +h+'');
            childWrep.document.write(content);
            childWrep.document.close();
            childWrep.name = 'repWin';
            window.childWrep = childWrep;
        } else {
            window.blur();
            if(window.childWrep.closed == false){
                childWrep.document.write(content);
                childWrep.document.close();
                setTimeout(function(){window.childWrep.focus()},1000);
            } else{
                var childWrep = window.open("", 'repWin','left=0,top=0,resizable=yes,status=yes,width='+w +',height=' +h+'');
                childWrep.document.write(content);
                childWrep.document.close();
                childWrep.name = 'repWin';
                window.childWrep = childWrep;
            }
        }
    }

并以这种方式将html放入这个子窗口。

        render : function() {

        var context = {
                data: data
                ...
        };

        this.openReportsWindow(this.template(context));

    }

这是问题所在。你如何从父窗口继承css样式?以及如何在子窗口中触发事件?

标签: javascriptjquerycssgoogle-chromeinternet-explorer

解决方案


要复制父样式,您可以使用 .style 属性,只需为父元素和子元素使用元素选择器,并将 child.style 属性分配给 parents.style。

例子:

let $parent = document.getElementById('parenID')
let $child = document.getElementById('childID')
$child.style = parent.style

推荐阅读