Selection.addRange()已弃用,将从Chrome中删除

Inc*_*c33 13 javascript google-chrome editor deprecated aloha-editor

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

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

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

Ray*_*dus 24

诀窍是在使用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.
Run Code Online (Sandbox Code Playgroud)

  • 这是一个令人难以置信的见解,在我读过的许多文档中都没有见过。谢谢你! (2认同)