在Javascript contenteditable div中插入文本

时间:2008-10-30 23:36:23

标签: javascript cursor contenteditable

我有一个问题,我需要在div中插入一个文本(字符串,可能有也可能没有html标签)。它必须是div而不是textarea。

有没有这样做?首先,我需要获取光标位置,然后将文本插入该位置。

它与函数insertAdjacentText类似,但它只能在标记之前或之后插入,并且只能在IE中使用。

请参阅此网址:http://yart.com.au/test/iframe.aspx。注意如何将光标定位在div中,我们需要在光标位置添加文本。

4 个答案:

答案 0 :(得分:11)

如果您只需要在当前选择/插入点插入HTML,那就可以了。脱离我的头脑(让你开始):

在IE中,使用document.selection.createRange()。pasteHTML(theNewHTML)。

在其他浏览器中,您应该能够使用document.execCommand(“InsertHTML”,...)。

我只在可编辑的iframe /文档中使用了这些技术,而不是div,但是如果div是聚焦的话,这些调用应该有效。我相信。

另一个答案警告说,如果你想要更精细的控制,就像在其他地方插入文字一样,它会变得丑陋。

答案 1 :(得分:7)

范围和选择对象

使用selection range objects 。它们包含有关当前选择的所有信息,以及它在节点树中的位置。

公平警告: 做你所描述的并不是完全不可能的,但是如果你非常喜欢,那么你将会陷入浏览器怪癖和不兼容性的深层次。你将不得不做很多对象检测,检查以确保你有一致的值和方法。在开发时逐步检查所有浏览器。祝你好运!

答案 2 :(得分:1)

这是一个跨浏览器的示例,改编自this answer以使用iframe。

function pasteHtmlAtCaret(html, selectPastedContent, iframe) {
    var sel, range;
    var win = iframe ? iframe.contentWindow : window;
    var doc = win.document;
    if (win.getSelection) {
        // IE9 and non-IE
        sel = win.getSelection();
        if (sel.getRangeAt && sel.rangeCount) {
            range = sel.getRangeAt(0);
            range.deleteContents();

            // Range.createContextualFragment() would be useful here but is
            // only relatively recently standardized and is not supported in
            // some browsers (IE9, for one)
            var el = doc.createElement("div");
            el.innerHTML = html;
            var frag = doc.createDocumentFragment(), node, lastNode;
            while ( (node = el.firstChild) ) {
                lastNode = frag.appendChild(node);
            }
            var firstNode = frag.firstChild;
            range.insertNode(frag);

            // Preserve the selection
            if (lastNode) {
                range = range.cloneRange();
                range.setStartAfter(lastNode);
                if (selectPastedContent) {
                    range.setStartBefore(firstNode);
                } else {
                    range.collapse(true);
                }
                sel.removeAllRanges();
                sel.addRange(range);
            }
        }
    } else if ( (sel = doc.selection) && sel.type != "Control") {
        // IE < 9
        var originalRange = sel.createRange();
        originalRange.collapse(true);
        sel.createRange().pasteHTML(html);
        if (selectPastedContent) {
            range = sel.createRange();
            range.setEndPoint("StartToStart", originalRange);
            range.select();
        }
    }
}

答案 3 :(得分:0)

您还可以使用insertImage的round-about方法。使用execCommand“insertImage”插入伪图像(或小图像),然后使用Javascript替换innerHTML以添加文本。

例如:

iFrame.focus()
iFrame.document.execCommand("insertImage", "", "fakeimage.jpg")
iFrame.document.body.innerHTML=iFrame.document.body.innerHTML.replace("<img src=\"fakeimage.jpg\">", "MY CUSTOM <b>HTML</b> HERE!")

而iFrame等于预期iFrame的id。或者,您可以使用document.getElementById(“IDofDIVlayer”)来使用DIV层.innerHTML

请务必在调用execCommand之前进行聚焦,否则可能无法在IE中使用。

此方法也适用于IE。测试

相关问题