[英]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.