简体   繁体   English

在嵌套的contentEditable之后设置光标位置

[英]Set Cursor position after nested contentEditable

I've got this markup 我有这个标记

<div contentEditable="true">
    Some other editable content
    <div class="field" contentEditable="false">
        <span class="label">This is the label</span>
        <span class="value" contentEditable="true">This is where the caret is</span>
    </div>
    <!-- This is where I want the Caret -->
</div>

The caret is currently in the .field span. 插入符号目前处于.field范围内。

I need to move it back out after the .field in the parent contentEditable . 我需要将它移到回退.field在父contentEditable

例

How can this be accomplished via javascript (with the use of jQuery if needed) ? 如何通过javascript(如果需要使用jQuery)来实现?

It will be trying to trigger it on a keydown event when the caret is in the .value span as shown. 当插入符号位于.value范围内时,它将尝试在keydown事件上触发它,如图所示。

I am assuming that you want to go to this next section. 我假设你想进入下一节。 To make it clear, I added a text "Here!" 为了说清楚,我添加了一个文本“Here!” in a demo to show you that it does goes to the end. 在演示中向您展示它确实走到了尽头。

In the below demo, press Enter key from the .field .value to go to the end. 在下面的演示中,按.field .value Enter键直到结束。

DEMO: http://jsfiddle.net/GLzKy/1/ 演示: http //jsfiddle.net/GLzKy/1/

Below is the function from https://stackoverflow.com/a/4238971/297641 which actually does all the work. 以下是https://stackoverflow.com/a/4238971/297641的功能,它实际上完成了所有工作。

$('.field .value').keydown(function (e) {
    if (e.which == 13) { //Enter Key
        e.preventDefault();            
        placeCaretAtEnd($(this).closest('.parent')[0]);
    }
});

/**
  This below function is from https://stackoverflow.com/a/4238971/297641
  All credits goes to the original author.
*/
function placeCaretAtEnd(el) {
    el.focus();
    if (typeof window.getSelection != "undefined"
            && typeof document.createRange != "undefined") {
        var range = document.createRange();
        range.selectNodeContents(el);
        range.collapse(false);
        var sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
    } else if (typeof document.body.createTextRange != "undefined") {
        var textRange = document.body.createTextRange();
        textRange.moveToElementText(el);
        textRange.collapse(false);
        textRange.select();
    }
}

Tested in FF, IE8 and Chrome 在FF,IE8和Chrome中测试过

Reference: https://stackoverflow.com/a/4238971/297641 参考: https //stackoverflow.com/a/4238971/297641

An update to the previous answer. 对上一个答案的更新。 http://jsfiddle.net/GLzKy/4/ http://jsfiddle.net/GLzKy/4/

placeCaretAtEnd($(this).parent().nextAll('.field').children('value'));

UPDATE UPDATE

Updated answer with correct answer in comments 在评论中更新了答案并给出了正确答

For moving directly after the current field, you might want to use range.setStartAfter and range.setEndAfter: http://jsfiddle.net/GLzKy/5 要在当前字段之后直接移动,您可能需要使用range.setStartAfter和range.setEndAfter: http//jsfiddle.net/GLzKy/5

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

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