我在HTML文本容器中有这个JavaScript mouseUp处理程序:
function checkTextSelection() {
var selection = window.getSelection();
console.dir(selection);
if (selection && !selection.isCollapsed && isSelectionInInputText(selection))) {
var shouldSwitchOffsets = selection.anchorOffset > selection.focusOffset;
var newSelection = {
anchorOffset: shouldSwitchOffsets ? selection.focusOffset : selection.anchorOffset,
focusOffset: shouldSwitchOffsets ? selection.anchorOffset : selection.focusOffset
};
console.log("Selection in input text");
model.selection(newSelection);
model.isTextSelected(true);
} else if (model.shouldPreventSelectionOverwrite()){
model.shouldPreventSelectionOverwrite(false);
} else {
console.log("Selection not in input text");
model.isTextSelected(false);
}
}
如您所见,我将选择对象立即打印到函数第二行的控制台中。通常,它可以正常工作(FF,Chrome,Edge)。但是,当我在Edge中选择文本的最后 n 个字符时,它就是对象:{ (...), anchorOffset: 1, (...), focusOffset: 355, (...) }
如何解决此问题?我仅在选择最后一个字符时才发现此问题,但我不能排除存在其他情况。
编辑
在IE11中没有发生此问题。