21

我最近在使用aloha 编辑器时在 chrome 的控制台日志中注意到以下消息:

aloha.js:14579 - Selection.addRange() 合并现有 Range 和指定 Range 的行为已弃用,并将在 2017 年 4 月左右在 M58 中删除。有关更多信息,请参阅https://www.chromestatus.com/features/6680566019653632细节。

在尝试寻找替代品时,除了他们将要删除它之外,我找不到任何东西,所以我想知道 Selection.addRange() 摆脱此消息的替代方法。

4

1 回答 1

38

诀窍是在使用removeAllRanges()添加新范围之前使用您的选择addRange(range)。这是一个使用它来选择所有内容的示例elem

selection = window.getSelection();    // Save the selection.
range = document.createRange();
range.selectNodeContents(elem);
selection.removeAllRanges();          // Remove all ranges from the selection.
selection.addRange(range);            // Add the new range.
于 2017-04-16T23:05:43.710 回答