【问题标题】:Selection.addRange() is deprecated and will be removed from ChromeSelection.addRange() 已弃用,将从 Chrome 中删除
【发布时间】:2017-04-06 16:18:45
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript google-chrome editor deprecated aloha-editor


    【解决方案1】:

    诀窍是在使用addRange(range) 添加新范围之前,在您的选择中使用removeAllRanges()。下面是一个使用它来选择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.
    

    【讨论】:

    • 这是我读过的许多文档中所没有的令人难以置信的见解。谢谢!
    猜你喜欢
    • 2016-05-09
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 2016-07-10
    相关资源
    最近更新 更多