javascript - ContentEditable div 在 JS fiddle 中无法正常工作

标签 javascript html contenteditable

在 JS fiddle 的 contenteditable div 的句子之间书写时,光标位置会移动到句子的末尾。

JSFiddle

最佳答案

使用向上键代替计时器事件。我在按键后使用了等待树秒。您可以更改它以增加或减少延迟。

            <script>

            var changed, timer,
            lastValue = '',
            div = $('#ce'),
            words = ['oele', 'geel', 'politie', 'foo bar'];

        function markWords() {
            var html = div.html().replace(/<\/?strong>/gi, ''),
                text = html.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' '),
                exp;
            $.each(words, function(i, word) {
                exp = new RegExp('\\b(' + word + ')\\b', 'gi');
                html = html.replace(exp, function(m) {
               // console.log('WORD MATCH:', m);
                return '<strong>' + m + '</strong>';
                });
            });
            //html = html.replace('&nbsp;', ' ').replace(/\s+/g, ' ');
        //console.log('HTML:', html);
        //console.log('----');
            div.html(html);
        }

        //setInterval(function() {
        //    var html = div.html();
        //    if ( lastValue !== html && html ) {
        //console.log(lastValue);
        //console.log(html);
        ////console.log('----');
        //        lastValue = html;
        //        markWords();
        //        setEndOfContenteditable(div[0]);
        //    }
        //}, 500);



        $("#ce").on('keyup', function() {
            clearTimeout(timer);  //clear any running timeout on key up
            timer = setTimeout(function() { //then give it a second to see if the user is finished
                //do 
                var html = div.html();
                if ( lastValue !== html && html ) {
            // console.log(lastValue);
            // console.log(html);
            //console.log('----');
                    lastValue = html;
                    markWords();
                    setEndOfContenteditable(div[0]);
                }        

            }, 3000);
        });

        function setEndOfContenteditable(contentEditableElement)
        {
            var range,selection;
            if(document.createRange)//Firefox, Chrome, Opera, Safari, IE 9+
            {
                range = document.createRange();//Create a range (a range is a like the selection but invisible)
                range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range
                range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
                selection = window.getSelection();//get the selection object (allows you to change selection)
                selection.removeAllRanges();//remove any selections already made
                selection.addRange(range);//make the range you have just created the visible selection
            }
            else if(document.selection)//IE 8 and lower
            { 
                range = document.body.createTextRange();//Create a range (a range is a like the selection but invisible)
                range.moveToElementText(contentEditableElement);//Select the entire contents of the element with the range
                range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
                range.select();//Select the range (make it the visible selection
            }
        }


        markWords();
        setEndOfContenteditable(div[0]);
        </script>

关于javascript - ContentEditable div 在 JS fiddle 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43084306/

相关文章:

javascript - 使用确认对话框防止单击事件,然后继续操作

javascript - 如何从 yadcf 列(搜索)过滤器中排除隐藏元素?

javascript - 创建规则以禁用表单中的某些选项

javascript - contenteditable .html() 正在编码

javascript - 光标在 WKWebView 中的位置,在 iOS 中有一个可编辑的段落

javascript - 克隆数据数组模板时累积的类

java - 如何使用 <s :property/> tag 执行 JavaScript

javascript - 我哪里出错了 - JAVASCRIPT

javascript - 如何纠正在以下情况下加载 HTML 页面内容时出现的问题?

javascript - 如何使范围偏移与多行 contenteditable div 中的 HTML 元素一起使用?