简体   繁体   English

在文本选择之前/之后添加元素

[英]Add element before/after text selection

I'm looking for function which allows me to build some element before or after selected text.我正在寻找 function,它允许我在选定文本之前或之后构建一些元素。 Something similar like this one javascript replace selection all browsers but for adding some content before or after selection instead of replacing it, like after() and before() jQuery methods.类似于这个javascript 替换所有浏览器的选择,但用于在选择之前或之后添加一些内容而不是替换它,例如after()before() jQuery 方法。 Should I use some DOM selection method, if yes which one?我应该使用某种 DOM 选择方法吗?如果是,是哪一种? Or does exist something easier to carry it out?或者是否存在更容易执行的方法?

Here's a pair of functions to do this. 这是一对执行此操作的功能。

Live example: http://jsfiddle.net/hjfVw/ 实例: http//jsfiddle.net/hjfVw/

Code: 码:

var insertHtmlBeforeSelection, insertHtmlAfterSelection;

(function() {
    function createInserter(isBefore) {
        return function(html) {
            var sel, range, node;
            if (window.getSelection) {
                // IE9 and non-IE
                sel = window.getSelection();
                if (sel.getRangeAt && sel.rangeCount) {
                    range = window.getSelection().getRangeAt(0);
                    range.collapse(isBefore);

                    // Range.createContextualFragment() would be useful here but is
                    // non-standard and not supported in all browsers (IE9, for one)
                    var el = document.createElement("div");
                    el.innerHTML = html;
                    var frag = document.createDocumentFragment(), node, lastNode;
                    while ( (node = el.firstChild) ) {
                        lastNode = frag.appendChild(node);
                    }
                    range.insertNode(frag);
                }
            } else if (document.selection && document.selection.createRange) {
                // IE < 9
                range = document.selection.createRange();
                range.collapse(isBefore);
                range.pasteHTML(html);
            }
        }
    }

    insertHtmlBeforeSelection = createInserter(true);
    insertHtmlAfterSelection = createInserter(false);
})();

In MSIE: collapse the given range and the use pasteHTML to insert the element 在MSIE中: 折叠给定范围并使用pasteHTML插入元素

Others: Also collapse the given Range and insert the element via insertNode 其他:还会折叠给定的Range并通过insertNode插入元素


Both collapse-methods accept an optional argument which defines to where you want to collapse to. 两个collapse-methods都接受一个可选参数,该参数定义了要折叠到的位置。 If you want to put the element at the end, collapse to the end, otherwise to the start. 如果要将元素放在最后,请折叠到最后,否则为开头。

function yourFunction() {
    const sel = window.getSelection ? window.getSelection() : document.selection.createRange()
    if (!sel) return false

    if (sel.getRangeAt) {
        const range = sel.getRangeAt(0)
        const text = range.toString()
        console.log(text)
        range.deleteContents()
        range.insertNode(document.createTextNode(`before text${text}after`))
    } else {
        sel.pasteHTML(`[s=спойлер]${sel.htmlText}after`)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM