Selection.addRange() is deprecated and will be removed from Chrome

佐手、 提交于 2019-11-27 02:41:52

问题


I recently noticed the following message in chrome's console log, while using aloha editor:

aloha.js:14579 - The behavior that Selection.addRange() merges existing Range and the specified Range is deprecated and will be removed in M58, around April 2017. See https://www.chromestatus.com/features/6680566019653632 for more details.

While trying to find a replacement, i couldn't find anything besides that they are going to remove it, so i would like to know what are the alternatives for Selection.addRange() to get rid of this message.


回答1:


The trick is to use removeAllRanges() on your selection before adding your new range using addRange(range). Here is an example when using it to select all content of 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.


来源:https://stackoverflow.com/questions/43260617/selection-addrange-is-deprecated-and-will-be-removed-from-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!