问题
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