我正在像here中那样使用此方法。我能够将选择范围扩展到英语(如文本)上的单词边界,但这在Unicode文本上表现得很奇怪。例如,我使用类似印地语的文本,有时会选择多个单词,有时会选择远离光标的单词。
在Firefox和Chrome中都观察到这种奇怪的行为。我的问题是如何处理Unicode文本以将选择范围扩展到单词边界。
示例代码:
function snapSelectionToWord() {
var sel;
// Check for existence of window.getSelection() and that it has a
// modify() method. IE 9 has both selection APIs but no modify() method.
if (window.getSelection && (sel = window.getSelection()).modify) {
sel = window.getSelection();
// if (!sel.isCollapsed) {
// Detect if selection is backwards
var range = document.createRange();
range.setStart(sel.anchorNode, sel.anchorOffset);
range.setEnd(sel.focusNode, sel.focusOffset);
var backwards = range.collapsed;
range.detach();
// modify() works on the focus of the selection
var endNode = sel.focusNode, endOffset = sel.focusOffset;
sel.collapse(sel.anchorNode, sel.anchorOffset);
if (backwards) {
//sel.modify("move", "backward", "character");
sel.modify("move", "forward", "word");
sel.extend(endNode, endOffset);
//sel.modify("extend", "forward", "character");
sel.modify("extend", "backward", "word");
} else {
sel.modify("move", "forward", "character");
sel.modify("move", "backward", "word");
sel.extend(endNode, endOffset);
sel.modify("extend", "backward", "character");
sel.modify("extend", "forward", "word");
}
//}
} else if ( (sel = document.selection) && sel.type != "Control") {
var textRange = sel.createRange();
if (textRange.text) {
textRange.expand("word");
// Move the end back to not include the word's trailing space(s),
// if necessary
while (/\s$/.test(textRange.text)) {
textRange.moveEnd("character", -1);
}
textRange.select();
}
}
}