如何获得SCEditor文本区域中的当前插入符位置? [英] How to get current caret position in a SCEditor textarea?

查看:55
本文介绍了如何获得SCEditor文本区域中的当前插入符位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上的jQuery上使用了SCEditor,我想知道如何相对于textarea/div的开始获取当前插入符号的位置?

I'm using SCEditor along jQuery in my website and I wonder how can I get the current caret position relative to the beginning of the textarea/div?

我尝试过类似的事情:

$J('#textarea').sceditor('instance').getRangeHelper().selectedRange().startOffset

但这只是给我相对于当前DOM对象的位置,而不是整个文本区域的位置.

but that only gives me the position relative to the current DOM object, not the entire textarea.

我要完成的工作是从文本区域中删除插入符号后的所有文本.也许还有另一种方法.

What I'm trying to accomplish is to remove all the text after the caret from the textarea. Maybe there is another way to do that.

谢谢

推荐答案

您可以使用 rangeHelper( ).saveRange()在选择的开始和结束处插入标记,然后从它们开始工作.

You can use rangeHelper().saveRange() to insert markers at the start and end of the selection and work from them.

例如:

var sceditor = $("textarea").sceditor("instance");

// Inserts spans with the ID #sceditor-end-start and #sceditor-end-marker
// at the start and end of the current selection
sceditor.getRangeHelper().saveRange();

// Get the DOM node for #sceditor-end-marker and remove all
// nextSiblings and parent nextSiblings from the editor
// which will remove everything after the end of the selection
var node = sceditor.getBody().find('#sceditor-end-marker').get(0);
while (node) {
    while (node.nextSibling)
        node.parentNode.removeChild(node.nextSibling);

    node = node.parentNode;
}

// Restores the selection back to the positions of the
// #sceditor-end-start and #sceditor-end-marker markers
sceditor.getRangeHelper().restoreRange();
sceditor.focus();

这篇关于如何获得SCEditor文本区域中的当前插入符位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆