在 Chrome 中拖放图像

发布于 2024-11-07 00:31:42 字数 2362 浏览 1 评论 0原文

我正在尝试编写一个脚本,用于在 Chrome 的 iframe 中拖放图像。

<html>
<head>
    <script language="JavaScript">

        function InsertImage(ev)
        {
            alert("drag function called");
            var _image = document.createElement("image");
            var _sel = _win.getSelection();
            if(!_sel.isCollapsed)
            {
                _sel.deleteFromDocument();
            }
            try{
                var _range = _sel.getRangeAt(0);
            }
            catch(e)
            {
                _range = _doc.createRange();
                alert("000000:::: "+e);
                alert("_range is ::"+_range);
            }
            if(!_range)
            {
                _range = _doc.createRange();
            }
            alert("range is ::::"+_range);
            try
            {
                _range.insertNode(_image);
            }
            catch(e)
            {
                alert("1111::  "+e);
            } 
            _range.insertNode(_image);


        }

        function init()
        {
            _iframe =   document.createElement("iframe");
            _iframe.id = "view";
            _iframe.style.height = "250px";
            _iframe.style.width = "600px";
            _iframe.style.top =   "20px";
            _iframe.style.left = "200px";
            _iframe.style.position = "absolute";
            _iframe.style.border = "2px solid lightBlue";
            document.body.appendChild(_iframe);
                _iframe.contentDocument.designMode="on";//No I18N
                _win = _iframe.contentWindow;
                _win.focus();
                _doc = _win.document; //making it global variable

                _doc.body.innerHTML = "<p>aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz</p>";
                _doc.addEventListener("dragover",InsertImage,false);

        }

    </script>
</head>
<body onLoad="init()">
    <div style="position:absolute;top:300px;left:100px ">
        <p> this si a testing doc.here, we test the things</p>
    </div>
</body>

是不工作.. 在 chrome 控制台中,我收到此错误消息:

未捕获的错误:WRONG_DOCUMENT_ERR:DOM 异常 4

我猜选择和范围有问题。

I'm trying to write a script for drag and drop of image in a iframe in Chrome.

<html>
<head>
    <script language="JavaScript">

        function InsertImage(ev)
        {
            alert("drag function called");
            var _image = document.createElement("image");
            var _sel = _win.getSelection();
            if(!_sel.isCollapsed)
            {
                _sel.deleteFromDocument();
            }
            try{
                var _range = _sel.getRangeAt(0);
            }
            catch(e)
            {
                _range = _doc.createRange();
                alert("000000:::: "+e);
                alert("_range is ::"+_range);
            }
            if(!_range)
            {
                _range = _doc.createRange();
            }
            alert("range is ::::"+_range);
            try
            {
                _range.insertNode(_image);
            }
            catch(e)
            {
                alert("1111::  "+e);
            } 
            _range.insertNode(_image);


        }

        function init()
        {
            _iframe =   document.createElement("iframe");
            _iframe.id = "view";
            _iframe.style.height = "250px";
            _iframe.style.width = "600px";
            _iframe.style.top =   "20px";
            _iframe.style.left = "200px";
            _iframe.style.position = "absolute";
            _iframe.style.border = "2px solid lightBlue";
            document.body.appendChild(_iframe);
                _iframe.contentDocument.designMode="on";//No I18N
                _win = _iframe.contentWindow;
                _win.focus();
                _doc = _win.document; //making it global variable

                _doc.body.innerHTML = "<p>aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz</p>";
                _doc.addEventListener("dragover",InsertImage,false);

        }

    </script>
</head>
<body onLoad="init()">
    <div style="position:absolute;top:300px;left:100px ">
        <p> this si a testing doc.here, we test the things</p>
    </div>
</body>

Is is not working..
In chrome console , I get this error message:

Uncaught Error: WRONG_DOCUMENT_ERR: DOM Exception 4

Something wrong with selection and range, i guess.

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

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

发布评论

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

评论(1

帅气称霸 2024-11-14 00:31:42

每当 DOM 操作方法尝试使用属于两个不同 DOMDocument 的节点时,就会引发 DOMException.WRONG_DOCUMENT_ERR 异常。您必须使用 DOMDocument.importNode 方法才能将 DOMNode 从一个文档导入到另一个文档。

我想你必须将图像节点导入到 iFrame 文档中,

例如(未经测试):

var nodeToImport = _doc.importNode(_image, true);
//nodeToImport can now be added to the second document
 _doc.appendChild(nodeToImport);

更多信息

A DOMException.WRONG_DOCUMENT_ERR exception is thrown whenever a DOM manipulation method tries to work with nodes that are part of two different DOMDocuments. You must use the DOMDocument.importNode method in order to import a DOMNode from one document to another.

I suppose you have to import the image-node to the iFrame-document

something like (untested):

var nodeToImport = _doc.importNode(_image, true);
//nodeToImport can now be added to the second document
 _doc.appendChild(nodeToImport);

more here

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