【问题标题】:Delete particular Text before and after the selected text javascript在所选文本javascript之前和之后删除特定文本
【发布时间】:2016-08-24 03:52:08
【问题描述】:

我想删除所选文本前后的一些特定文本。例如,如果文本是:

<p>This is a &lt;random>sentence&lt;/random> that i am writing<p>

如果用户选择文本,它应该从文本中删除 ,文本将是这样的。

This is a sentence that i am writing.

如果用户选择“句子”以外的任何内容,则不会发生任何事情。 我知道如何选择特定文本,但我不知道下一步如何删除特定文本之前和之后的文本。有可能吗?

【问题讨论】:

    标签: javascript selectedtext


    【解决方案1】:

    function replaceSelection() {
        var sel, range, fragment;
    
        if (typeof window.getSelection != "undefined") {
            // IE 9 and other non-IE browsers
            sel = window.getSelection();
    
            // Test that the Selection object contains at least one Range
            if (sel.getRangeAt && sel.rangeCount) {
                // Get the first Range (only Firefox supports more than one)
                range = window.getSelection().getRangeAt(0);
                var selectedText = range.toString();
                var replacementText = selectedText.replace(/&lt;\/?random>/, '');
                range.deleteContents();
    
                // Create a DocumentFragment to insert and populate it with HTML
                // Need to test for the existence of range.createContextualFragment
                // because it's non-standard and IE 9 does not support it
                if (range.createContextualFragment) {
                    fragment = range.createContextualFragment(replacementText);
                } else {
                    // In IE 9 we need to use innerHTML of a temporary element
                    var div = document.createElement("div"), child;
                    div.innerHTML = replacementText;
                    fragment = document.createDocumentFragment();
                    while ( (child = div.firstChild) ) {
                        fragment.appendChild(child);
                    }
                }
                var firstInsertedNode = fragment.firstChild;
                var lastInsertedNode = fragment.lastChild;
                range.insertNode(fragment);
                if (selectInserted) {
                    if (firstInsertedNode) {
                        range.setStartBefore(firstInsertedNode);
                        range.setEndAfter(lastInsertedNode);
                    }
                    sel.removeAllRanges();
                    sel.addRange(range);
                }
            }
        } else if (document.selection && document.selection.type != "Control") {
            // IE 8 and below
            range = document.selection.createRange();
            var selectedText = range.text;
            var replacementText = selectedText.replace(/&lt;\/?random>/, '')        
            range.pasteHTML(replacementText);
        }
    }
    &lt;div onmouseup="replaceSelection()"&gt;&lt;p&gt;This is a &amp;lt;random&gt;sentence&amp;lt;/random&gt; that i am writing&lt;p&gt;&lt;/div&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-03
      • 2019-08-01
      • 2020-01-27
      • 1970-01-01
      • 2021-12-19
      相关资源
      最近更新 更多