JavaScript Set Window选择 [英] JavaScript Set Window selection

查看:235
本文介绍了JavaScript Set Window选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,有一个方法 window.getSelection(),它允许我获取用户所做的当前选择。

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

是否有相应的函数,比如 window.setSelection(),这将让我设置或清除当前选择?

Is there a corresponding function, something like window.setSelection(), that will let me set, or clear, the current selection?

推荐答案

也许会这样做:

window.selection.clear();

Crossbrowser版本:

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();
}

这篇关于JavaScript Set Window选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆