简体   繁体   English

获取window.getSelection()之后的下一个字符

[英]Getting the next character after window.getSelection()

Is there anyway to get the next character after a window.getSelection()? 无论如何在window.getSelection()之后获取下一个字符? I need to check if the character after the selected text is a space or not... 我需要检查所选文本后面的字符是否是空格...

EDIT: Thank you for your answers! 编辑:谢谢你的答案! I'm basically using this link to highlight text, but would like to limit things to full words. 我基本上使用此链接来突出显示文本,但希望将内容限制为完整的单词。 I've used the presented solution below (by Steven) as a starting point; 我已经使用下面提出的解决方案(由Steven)作为起点; I think that the following should work: 认为以下应该有效:

sel = window.getSelection();
var text = sel.anchorNode.nodeValue;
var index = sel.baseOffset + sel.focusOffset-1;
var isSpace = text[index] === undefined;
if (isSpace) {
alert("space");
}

(In the link above, I used this code right after the makeEditableAndHighlight function call). (在上面的链接中,我在makeEditableAndHighlight函数调用之后立即使用了此代码)。

这是一个开始,前提是focusNode中至少还有一个字符:

window.getSelection().focusNode.textContent.charAt(window.getSelection().focusOffset)

Try it this way: 试试这种方式:

var sel = window.getSelection()
var text = sel.anchorNode.nodeValue;
var index = sel.baseOffset + sel.focusOffset;
var isSpace = text[index] === ' ';

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM