Internet Explorer Dom 元素 getElementById

发布于 2024-12-15 03:51:14 字数 676 浏览 6 评论 0原文

在 Internet Explorer 8 下,当我尝试将 Div 从窗口复制到弹出窗口时,getElementById 方法不会返回 DOM 元素,而是返回普通元素。我无法对此类项目调用appendChild,因为我收到“非法参数”错误。将 div 从父窗口复制到子弹出窗口的解决方案是什么?

到目前为止,我编写的代码可以在 Chrome 和 Firefox 上完美运行,但不能在 IE 上运行。

此代码位于弹出窗口中:

加载弹出窗口时调用此代码

<body onload="initialize();">

     <div id='sourceDiv'></div>

</body>

function initialize(){
    var source = window.opener.document; 
    var myDiv = source.getElementById("myDiv"); 
    var destination = document.getElementById("sourceDiv");
    destination.appendChild(myDiv); 
}

以下代码段来自父窗口

<div id='myDiv>
...
</div> 

Under Internet Explorer 8, when I try to copy a Div from a window to a pop-up the getElementById method doesn't return DOM elements but plain elements. I can't call appendChild on those sort of items cause I get "Illegal argument" errors. What would be a solution of copying a div from a parrent window to a child pop-up window.

The code I've written so far works perfectly on Chrome, and Firefox, but not on IE.

this code is in the pop-up window:

this is called on when the pop-up is loading

<body onload="initialize();">

     <div id='sourceDiv'></div>

</body>

function initialize(){
    var source = window.opener.document; 
    var myDiv = source.getElementById("myDiv"); 
    var destination = document.getElementById("sourceDiv");
    destination.appendChild(myDiv); 
}

the following snippet is from the parrent window

<div id='myDiv>
...
</div> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄人书 2024-12-22 03:51:14

为什么不在 jQuery 中这样做呢? jQuery 被设计为独立于浏览器。

jQuery 代码将是这样的一行:

$('#sourceDiv').append($('#myDiv').html());

好的,我刚刚尝试了一个测试项目,并且此代码有效:

<script language="javascript">

    $(document).ready(function() {
        $('#sourceDiv').append($('#myDiv').html());
    });
</script>


<body>
    <div id='sourceDiv'></div>
    <div id="myDiv">myDiv text</div>
</body>

您需要确保在调用函数时加载文档。

Why dont you do this in jQuery? jQuery is designed to be browser independent.

jQuery code would be one line like so:

$('#sourceDiv').append($('#myDiv').html());

Ok I've just tried a test project and this code works:

<script language="javascript">

    $(document).ready(function() {
        $('#sourceDiv').append($('#myDiv').html());
    });
</script>


<body>
    <div id='sourceDiv'></div>
    <div id="myDiv">myDiv text</div>
</body>

You need to make sure the document is loaded when you call your function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文