简体   繁体   English

如何获得SCEditor文本区域中的当前插入符位置?

[英]How to get current caret position in a SCEditor textarea?

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

I tried things like: 我尝试过类似的事情:

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

but that only gives me the position relative to the current DOM object, not the entire textarea. 但这只给了我相对于当前DOM对象的位置,而不是整个文本区域的位置。

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. 也许还有另一种方法可以做到这一点。

Thanks, 谢谢,

You can use rangeHelper().saveRange() to insert markers at the start and end of the selection and work from them. 您可以使用rangeHelper()。saveRange()在选择的开始和结束处插入标记,然后从它们开始工作。

eg: 例如:

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

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

相关问题 如何在textarea中的当前插入位置插入文本 - How to insert text at the current caret position in a textarea 获取文本区域(IE)中的插入符位置 - Get caret position in textarea (IE) 如何从一开始就以字符形式在textarea中获取插入符号列(不是像素)的位置? - How to get the caret column (not pixels) position in a textarea, in characters, from the start? Javascript:如何获取textarea中的line / col插入符号位置? - Javascript: how to get line/col caret position in textarea? 如何使用contentEditable从iframe中的当前插入位置获取像素偏移量 - How to get the pixel offset from the current caret position in an iframe with contentEditable 如何从sceditor textarea结果中删除标签 - How to remove tags from sceditor textarea result 通过光标/插入符号位置获取当前句子 - Get current sentence by cursor/caret position Javascript获取输入文字或文本区域中插入符号的绝对位置(以像素为单位) - Javascript get the absolute position (in px) of a caret in a input text or textarea Tinymce - 是否有可能获得与所见即所得编辑器对应的textarea的插入位置? - Tinymce - Is it possible to get caret position of textarea corresponding to WYSIWYG Editor? 如何在SCeditor中移动cursor position? - How to move the cursor position within SCeditor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM