将孩子添加到弹出窗口中.(JavaScript) [英] Appending children into a popup-window. (JavaScript)

查看:60
本文介绍了将孩子添加到弹出窗口中.(JavaScript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使一个相当简单的popupper正常工作时遇到了一些麻烦.想法是,父级应该打开一个弹出窗口,然后在其中添加一个div.

I'm having some trouble trying to get a fairly simple popupper to work. The idea is that the parent should open a popup window and then append a div in it.

代码的相关部分:

parent.html :

var childWindow;

function togglePref() {
    childWindow = window.open("popup.html", "prefPopup", "width=200,height=320");
}

function loadPopupElements() {
    var prefElements = document.getElementById("prefBrd").cloneNode(true);

    var childDoc = childWindow.document;

    var childLink = document.createElement("link");
    childLink.setAttribute("href", "pop.css");
    childLink.setAttribute("rel", "stylesheet");
    childLink.setAttribute("type", "text/css");

    childDoc.head.appendChild(childLink);
    childDoc.body.appendChild(prefElements);
}

popup.html :

<head>
</head>

<body onload="opener.loadPopupElements();">
</body>

这在Safari和Chrome上正常运行,但是由于某些原因IE拒绝附加任何内容.

This works fine with Safari and Chrome, but for some reason IE refuses to append anything.

推荐答案

好的,我设法通过使用innerHTML的丑陋解决方案来解决该问题.显然,正如Hemlock提到的那样,IE不支持从另一个文档中追加子代.有些人建议看一下importNode()方法,但我似乎也没有运气.

Ok, I managed to work around the problem with a uglyish solution using innerHTML. Apparently, as Hemlock mentioned, IE doesn't support appending children from a another document. Some suggested to take a look at the importNode() method but I seemed to have no luck with it either.

因此,解决方法如下:

parent.html :

var childWindow;

function togglePref() {
    childWindow = window.open("popup.html", "prefPopup", "width=200,height=320");
}

function loadPopupElements() {
    var prefElements = document.getElementById("prefBrd");
    var childDoc = childWindow.document;
    childDoc.body.innerHTML = prefElements.innerHTML;
}

popup.html :

<head>
    <link href="pop.css" rel="stylesheet" type="text/css">
</head>

<body onload="loadElements();">
</body>

<script type="text/javascript">
    function loadElements() {
        opener.loadPopupElements();
    }
</script>

这似乎是一个令人讨厌的方法,因为在我的案例中, #prefBrd 包含一些具有动态设置值的输入元素,因此为了使popup.html能够抓住它们,它必须在loadElements()函数末尾进行一些迭代,而使用appendChild则不需要.

This seems quite a nasty way to go because in my case the #prefBrd contains some input elements with dynamically set values, so in order for the popup.html to grab them, it has to do a bit of iteration at the end of the loadElements() function, which wouldn't have been necessary using appendChild.

这篇关于将孩子添加到弹出窗口中.(JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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