javascript chrome 支持 document.selection 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7003784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:39:28  来源:igfitidea点击:

Does chrome supports document.selection?

javascriptgoogle-chrome

提问by adi

I am new to javascript, trying to perform:

我是 javascript 新手,试图执行:

document.selection.createRange();

but document.selectionalways returns undefined.

document.selection总是返回 undefined

I am using latest version of chrome.

我正在使用最新版本的 chrome。

what am I doing wrong?

我究竟做错了什么?

thanks!

谢谢!

回答by Tim Down

Use window.getSelection(), which is the most cross-browser compatible (it's supported in the current versions of all major browsers) and is the standard. Chrome certainly supports it as fully as other browsers.

使用window.getSelection(),它是最具跨浏览器兼容性的(所有主要浏览器的当前版本都支持它)并且是标准的. Chrome 当然像其他浏览器一样完全支持它。

document.selectionshould only be used for IE < 9.

document.selection应该只用于 IE < 9。

回答by Garett

Try document.getSelection()or window.getSelection().

尝试document.getSelection()window.getSelection()

Here's a quick example that I tested in chrome

这是我在 chrome 中测试的一个快速示例

http://jsfiddle.net/hgDwx/

http://jsfiddle.net/hgDwx/

回答by Jeremy