[英]How to get current caret position in a SCEditor textarea?
我在网站上的jQuery中使用了SCEditor,我想知道如何相对于textarea / div的开始获取当前插入符号的位置?
我尝试过类似的事情:
$J('#textarea').sceditor('instance').getRangeHelper().selectedRange().startOffset
但这只给了我相对于当前DOM对象的位置,而不是整个文本区域的位置。
我要完成的是从文本区域中删除插入符号后的所有文本。 也许还有另一种方法可以做到这一点。
谢谢,
您可以使用rangeHelper()。saveRange()在选择的开始和结束处插入标记,然后从它们开始工作。
例如:
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.