是否有使用JavaScript取消选择所有文本的功能? [英] Is there a function to deselect all text using JavaScript?

查看:98
本文介绍了是否有使用JavaScript取消选择所有文本的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javascript中是否有一个函数可以取消选择所有选定的文本?我认为它必须是一个简单的全局函数,如 document.body.deselectAll(); 或其他东西。

Is there a function in javascript to just deselect all selected text? I figure it's got to be a simple global function like document.body.deselectAll(); or something.

推荐答案

试试这个:

function clearSelection()
{
 if (window.getSelection) {window.getSelection().removeAllRanges();}
 else if (document.selection) {document.selection.empty();}
}

这将清除任何主要浏览器中常规HTML内容的选择。它不会清除文本输入中的选择或Firefox中的< textarea>

This will clear a selection in regular HTML content in any major browser. It won't clear a selection in a text input or <textarea> in Firefox.

这篇关于是否有使用JavaScript取消选择所有文本的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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