2

我正在尝试插入引号,然后将光标移动到编辑器的末尾。目前,我插入引号,光标最终在引号内,所以如果您要开始输入,用户将添加到引号中,这不是我想要发生的

我已经尝试查看他们的文档以弄清楚如何做到这一点,但我认为我引用的不是理解它

这是我当前的代码:

    $(document.body).on('click', '.action.quote', function() {

        var $target = $($(this).data('target'));
        var editor = $('#reply-body').sceditor('instance');
        var bodyText = $target.html();

        editor.insert('[quote=author]' + bodyText + '[/quote]');
        editor.focus();

        smoothScroll('#new-comment');
    });

有没有快速的方法来做到这一点?我对 SCEditor 很陌生,所以对我来说都是新的

4

1 回答 1

2

目前没有一种简单的方法可以将光标放在插入的内容之后,尽管可能应该有。我将创建一个问题来添加一个。

现在,您需要使用 range API 手动移动光标。像这样的东西应该工作:

$(document.body).on('click', '.action.quote', function() {
    var $target = $($(this).data('target'));
    var editor = $('#reply-body').sceditor('instance');
    var bodyText = 'Dummy text for example';

    editor.insert('[quote=author]' + bodyText + '[/quote]');
    editor.focus();

    var rangeHelper = editor.getRangeHelper();
    var range = rangeHelper.cloneSelected();

    // Handle DOM ranges, if you casre about IE < 9
    // you'll need to handle TextRanges too
    if ('selectNodeContents' in range) {
        var bodyChildren = editor.getBody()[0].children;

        // Select the last child of the body
        range.selectNodeContents(bodyChildren[bodyChildren.length - 1]);

        // Move cursor to after it
        range.collapse(false);
        rangeHelper.selectRange(range);
    }

    smoothScroll('#new-comment');
});

现场示例:https ://jsfiddle.net/3z8cg8z1/

于 2017-04-20T12:44:56.430 回答