【问题标题】:Replace certain last words typed with html in contenteditable div在 contenteditable div 中替换某些最后用 html 键入的单词
【发布时间】:2016-05-29 02:24:16
【问题描述】:

我正在尝试在用户键入时用超链接动态替换内容可编辑 div 中的某些关键字。我通过首先将整个字符串用“”拆分,然后抓取最近的单词,如果它是我的关键字之一,我在整个字符串中找到开始索引和结束索引,然后执行以下操作,我可以很好地处理他们键入的第一个单词:

range.setStart(myDiv.firstChild, startOfWordIndex);
range.setEnd(myDiv.firstChild, endOfWordIndex);
range.deleteContents();
range.insertNode(...);

我插入的节点是我创建的超链接,但为简洁起见,这里没有全部输入。

我的问题是,插入该节点后,我无法再在 setStart() 方法中使用 myDiv.firstChild,因为我在用户输入的位置前面有了新节点。

这是我对内容可编辑 html 的第一次破解,所以我不确定如何抓取最后一个节点,也不确定使用我的单词的开始和结束索引无论如何都可以在那里工作,因为这些是基于整个div 内容的长度。

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript jquery range selection wysihtml5


    【解决方案1】:

    睡了一会儿,我自己解决了这个问题:大量评论以防万一可以帮助别人。

    function replaceLastWordWithLink(editContent) {
        var selection, selectedRange, range, node, frag, el, selectionText, wordStart, wordEnd, currentWord;
        // set the selection
        selection = window.getSelection();
        // set the range by the cursor
        selectedRange = selection.getRangeAt(0);
        // set the "global" range
        range = document.createRange();
        // get all node contents of global range
        range.selectNodeContents(editContent);
        // get the node the cursor is in
        node = selectedRange.startContainer;
        // point the global range to node the cusor is in and start of 0
        range.setStart(node, 0);
        // point the global range to node the cursor is in and end of where cursor is
        range.setEnd(node, selectedRange.startOffset);
        // create the fragment for the contents
        frag = range.cloneContents();
        // create a pseudo element to place the fragment in
        el = document.createElement("span");
        // place fragment in pseudo element
        el.appendChild(frag);
        // get the text from the pseduo element
        selectionText = el.innerText;
        // pattern to see if there are spaces
        spacePattern = /\s/;
        if (!spacePattern.test(selectionText)) {
            // no spaces so the start of the word index is at 0
            wordStart = 0;
            // no spaces so end of the word index is just where the cusor is (the total length of the text)
            wordEnd = selectionText.length;
        } else {
            // split off the last word in the text
            currentWord = selectionText.split(/\s/).reverse()[0].toLowerCase();
            // get the start of the word's index in the string
            wordStart = selectionText.lastIndexOf(currentWord);
            // get the end of the word's index by adding start of word index to the word length
            wordEnd = wordStart + currentWord.length;
        }
        // now set the range to the current word
        range.setStart(node, wordStart);
        range.setEnd(node, wordEnd);
        // now remove the current word
        range.deleteContents();
        // now replace the word with the link
        var el = document.createElement("a");
        el.href = "http://www.yahoo.com";
        el.text = selectedText;
        range.insertNode(el);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多