Contenteditable/jQuery/Javascript-选择从光标/插入符号到段落末尾的文本

Contenteditable/jQuery/Javascript-选择从光标/插入符号到段落末尾的文本,javascript,jquery,contenteditable,Javascript,Jquery,Contenteditable,当按下回车键时,我正在尝试写一个新段落。我能做到,而且效果很好。但是现在假设光标位于段落的中间-当按下enter键时,我试图选择一个从光标位置到段落末尾的范围。将其从现有段落中删除,并添加到下面的新段落中 我正在尝试修改答案中的代码: $(document).on('keydown',p[contenteditable=“true”]”,函数(e){ 如果(e.which==13){//输入/返回新段落 e、 预防默认值(); var sel=window.getSelection(); 如果(

当按下回车键时,我正在尝试写一个新段落。我能做到,而且效果很好。但是现在假设光标位于段落的中间-当按下enter键时,我试图选择一个从光标位置到段落末尾的范围。将其从现有段落中删除,并添加到下面的新段落中

我正在尝试修改答案中的代码:

$(document).on('keydown',p[contenteditable=“true”]”,函数(e){
如果(e.which==13){//输入/返回新段落
e、 预防默认值();
var sel=window.getSelection();
如果(选择范围计数){
var selRange=sel.getRangeAt(0);
var blockEl=selRange.endContainer.parentNode;
var range=selRange.cloneRange();
范围。选择节点内容(blockEl);
range.setStart(selRange.endContainer、selRange.endOffset);
remainingText=range.extractContents();
$(this).after(“

”+remainingText+”

); $(this.next('p').focus(); }
我没有取得多大成功-主要是因为我对范围、节点和选择对象缺乏理解。有人能解释一下这些对象是如何工作的,以及我如何调整上述答案以适应我的情况吗


以下是一些修改后的代码,用于删除Rangy依赖项:

var sel = window.getSelection();
if (sel.rangeCount > 0) {
    // Create a copy of the selection range to work with
    var range = sel.getRangeAt(0).cloneRange();

    // Get the containing paragraph
    var p = range.commonAncestorContainer;
    while (p && (p.nodeType != 1 || p.tagName != "P") ) {
        p = p.parentNode;
    }

    if (p) {
        // Place the end of the range after the paragraph
        range.setEndAfter(p);

        // Extract the contents of the paragraph after the caret into a fragment
        var contentAfterRangeStart = range.extractContents();

        // Collapse the range immediately after the paragraph
        range.setStartAfter(p);
        range.collapse(true);

        // Insert the content
        range.insertNode(contentAfterRangeStart);

        // Move the caret to the insertion point
        range.setStartAfter(p);
        range.collapse(true);
        sel.removeAllRanges();
        sel.addRange(range);
    }
}

下面是一些改编自的代码,用于删除Rangy依赖项:

var sel = window.getSelection();
if (sel.rangeCount > 0) {
    // Create a copy of the selection range to work with
    var range = sel.getRangeAt(0).cloneRange();

    // Get the containing paragraph
    var p = range.commonAncestorContainer;
    while (p && (p.nodeType != 1 || p.tagName != "P") ) {
        p = p.parentNode;
    }

    if (p) {
        // Place the end of the range after the paragraph
        range.setEndAfter(p);

        // Extract the contents of the paragraph after the caret into a fragment
        var contentAfterRangeStart = range.extractContents();

        // Collapse the range immediately after the paragraph
        range.setStartAfter(p);
        range.collapse(true);

        // Insert the content
        range.insertNode(contentAfterRangeStart);

        // Move the caret to the insertion point
        range.setStartAfter(p);
        range.collapse(true);
        sel.removeAllRanges();
        sel.addRange(range);
    }
}
子字符串版本:

$(document).on('keydown', 'p[contenteditable="true"]', function(e) {
    if(e.which == 13) { //new paragraph on enter/return
        e.preventDefault();
        cursorIndex = window.getSelection().getRangeAt(0).startOffset;
        textBefore = $(this).text().substring(0, cursorIndex);
        textAfter = $(this).text().substring(cursorIndex);
        $(this).text(textBefore); 
        $(this).after('<p contenteditable = "true">'+ textAfter +'</p>');
        $(this).next('p').focus();

    }
}
$(document).on('keydown',p[contenteditable=“true”]”,函数(e){
如果(e.which==13){//输入/返回新段落
e、 预防默认值();
cursorIndex=window.getSelection().getRangeAt(0).startOffset;
textBefore=$(this.text().substring(0,cursorIndex);
textAfter=$(this.text().substring(cursorIndex);
$(this).text(textBefore);
$(this).after(“

”+textfafter+”

”); $(this.next('p').focus(); } }
子字符串版本:

$(document).on('keydown', 'p[contenteditable="true"]', function(e) {
    if(e.which == 13) { //new paragraph on enter/return
        e.preventDefault();
        cursorIndex = window.getSelection().getRangeAt(0).startOffset;
        textBefore = $(this).text().substring(0, cursorIndex);
        textAfter = $(this).text().substring(cursorIndex);
        $(this).text(textBefore); 
        $(this).after('<p contenteditable = "true">'+ textAfter +'</p>');
        $(this).next('p').focus();

    }
}
$(document).on('keydown',p[contenteditable=“true”]”,函数(e){
如果(e.which==13){//输入/返回新段落
e、 预防默认值();
cursorIndex=window.getSelection().getRangeAt(0).startOffset;
textBefore=$(this.text().substring(0,cursorIndex);
textAfter=$(this.text().substring(cursorIndex);
$(this).text(textBefore);
$(this).after(“

”+textfafter+”

”); $(this.next('p').focus(); } }
试试这个

      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();
        }
    }
试试这个

      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();
        }
    }

您可以获取光标的字符位置…从该位置开始获取当前文本的子字符串,然后创建一个新的{您可以}然后在那里添加这个内容。好主意,我来试一试。你可以得到光标的字符位置…从这个位置开始得到当前文本的子字符串,然后创建一个新的{你可以}然后在那里添加这些内容。好主意,我来试一试。我想你在靶场和所有领域都是权威……不久前,我在你的一个问题上发了帖子,如果你能告诉我如何找到光标的行号。遗憾的是,你从来没有回复过。@BhumiSinghal:对不起,我没有回复。我花在StackO上的时间有限最近verflow和Rangy,所以我一直没有跟上。我猜你在靶场和所有方面都是权威……不久前,我在你的一个问题上发了帖子,如果你能告诉我如何找到光标的行号。很遗憾,你从来没有回复过。@BhumiSinghal:对不起,我没有回复。我花在StackOverf上的时间有限最近又矮又瘦,所以我一直跟不上。