Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在选定文本的前后插入文本_Javascript_Textselection_Getselection - Fatal编程技术网

Javascript 在选定文本的前后插入文本

Javascript 在选定文本的前后插入文本,javascript,textselection,getselection,Javascript,Textselection,Getselection,可能重复: 我想在HTML文档中的任何选定文本之前和之后放置一些指定文本(如果可能,在启用设计模式时在iframe中)。document.getSelection()或document.selection.createRange().text只返回文本本身,而不返回位置 是否仍有替换所选文本的方法 是否在文档中的任意位置插入选定文本前后的特定文本?我今天早些时候回答了您的一个相关问题: 此外,我在一年前发布了一个非常类似的问题的答案,最近更新为使用IE 9: 最后,这里是可编辑iframe

可能重复:

我想在HTML
文档中的任何选定文本之前和之后放置一些指定文本(如果可能,在启用设计模式时在iframe中)。document.getSelection()
document.selection.createRange().text
只返回文本本身,而不返回位置

是否仍有替换所选文本的方法


是否在文档中的任意位置插入选定文本前后的特定文本?

我今天早些时候回答了您的一个相关问题:

此外,我在一年前发布了一个非常类似的问题的答案,最近更新为使用IE 9:

最后,这里是可编辑iframe的第二个链接答案中的函数版本。它允许您指定文档对象:

function insertHtmlAtSelectionEnd(html, isBefore, doc) {
    doc = doc || document;
    var win = doc.defaultView || doc.parentWindow;
    var sel, range, node;
    if (win.getSelection) {
        sel = win.getSelection();
        if (sel.getRangeAt && sel.rangeCount) {
            range = sel.getRangeAt(0);
            range.collapse(isBefore);

            // Range.createContextualFragment() would be useful here but was
            // until recently non-standard and not supported in all 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);
            }
            range.insertNode(frag);
        }
    } else if ( (sel = doc.selection) && sel.type != "Control") {
        range = sel.createRange();
        range.collapse(isBefore);
        range.pasteHTML(html);
    }
}