Set cursor after span element inside contenteditable div

后端 未结 4 1206
旧时难觅i
旧时难觅i 2020-12-03 12:09

How can I set the cursor after a span element inside an contenteditable div? Right now, I\'ve an image inside a span element, inside the contenteditable div.

When I

相关标签:
4条回答
  • 2020-12-03 12:22

    In browsers other than IE <= 8, this will do it:

    function placeCaretAfterNode(node) {
        if (typeof window.getSelection != "undefined") {
            var range = document.createRange();
            range.setStartAfter(node);
            range.collapse(true);
            var selection = window.getSelection();
            selection.removeAllRanges();
            selection.addRange(range);
        }
    }
    

    However, some browsers (notably WebKit-based ones) have fixed ideas about which positions in the document are valid for the caret and will normalize any range you add to the selection to comply with those ideas. The following example will do what you want in Firefox and IE 9 but not in Chrome or Safari as a result:

    http://jsfiddle.net/5dxwx/

    None of the workarounds are good. Options include:

    • add a zero-width space character after the span and select it
    • use an <a> element instead of a <span> because WebKit makes an exception for <a> elements
    0 讨论(0)
  • 2020-12-03 12:29

    This function manages what I had in mind:

    <script type="text/javascript">
    function pasteHtmlAtCaret(char) {
            var sel = rangy.getSelection();
            var range = sel.rangeCount ? sel.getRangeAt(0) : null;
            var parent;
            if (range) {
                var parentEl = document.createElement("span");
            jQuery(parentEl).addClass('char').addClass('latest');
                parentEl.appendChild(range.createContextualFragment(char));
    
                // Check if the cursor is at the end of the text in an existing span
                if (range.endContainer.nodeType == 3
                && (parent = range.endContainer.parentNode)
                && (parent.tagName == "SPAN")) {
                range.setStartAfter(parent);
                }
                range.insertNode(parentEl);
            if(jQuery(parentEl).parent().get(0).tagName.toLowerCase() == 'span'){
                var spanParent = jQuery(parentEl).parent('span');
                var parentEl = jQuery('<span class="char latest spanchild"></span>').insertAfter(spanParent).append(parentEl).get(0);
            }
            jQuery("span.spanchild").each(function(index, element){
                var content = jQuery(element).children('span.char').first().html();
                jQuery(element).children('span.char').remove();
                jQuery(element).html(content);
                jQuery(element).removeClass('spanchild');
            });
                range.setStartAfter(parentEl);
                rangy.getSelection().setSingleRange(range);
            jQuery(parentEl).removeClass('latest');
            }
    }
    </script>
    
    0 讨论(0)
  • 2020-12-03 12:40

    I managed to solve this problem using 'a' tag with &nbsp. Example :- "<a>"+ content/element you want to add +"</a>&nbsp;"

    0 讨论(0)
  • 2020-12-03 12:40

    Just span.contentEditable = false to Tim's solution work properly ;)

    0 讨论(0)
提交回复
热议问题