JavaScript Set Window selection

前端 未结 3 1551
无人共我
无人共我 2020-12-05 03:23

In JavaScript, there is a method window.getSelection(), that lets me get the current selection that the user has made.

Is there a corresponding function

3条回答
  •  温柔的废话
    2020-12-05 04:06

    Maybe this will do it:

    window.selection.clear();
    

    Crossbrowser version:

    if (window.getSelection) {
       if (window.getSelection().empty) {  // Chrome
         window.getSelection().empty();
       } else if (window.getSelection().removeAllRanges) {  // Firefox
         window.getSelection().removeAllRanges();
       }
    } else if (document.selection) {  // IE?
      document.selection.empty();
    }
    

提交回复
热议问题