我正在尝试编写一个脚本,用于在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
选择和范围有问题,我想。
答案 0 :(得分:1)
当DOM操作方法尝试使用与两个不同的DOMDocuments 一起使用的节点时,会抛出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);