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
Does chrome supports document.selection?
提问by adi
I am new to javascript, trying to perform:
我是 javascript 新手,试图执行:
document.selection.createRange();
but document.selection
always 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.selection
should 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 中测试的一个快速示例
回答by Jeremy
Use window.getSelection()
instead.
使用window.getSelection()
来代替。