JavaScript/jQuery:获取选择功能在Firefox和Chrome中不起作用
JavaScript / jQuery: get selection function not working in Firefox and Chrome
我使用以下函数在可编辑内容的div中获取所选文本(即用户选择的文本)。这在IE 9中非常适用,但在IE 8、Firefox或Chrome(均为最新版本)中则不然。
这里有人能帮我修改一下吗,至少在Firefox和IE 8中也能使用(Chrome不是必须的)?
我的功能(工作):
function GetSelection()
{
selTxt = '';
if (typeof window.getSelection != "undefined")
{
var sel = window.getSelection();
if (sel.rangeCount)
{
var container = document.createElement('div');
for (var i = 0, len = sel.rangeCount; i < len; ++i)
{
container.appendChild(sel.getRangeAt(i).cloneContents());
}
selTxt = container.innerHTML;
}
}
else if (typeof document.selection != 'undefined')
{
if (document.selection.type == 'Text')
{
selTxt = document.selection.createRange().htmlText;
}
}
return selTxt;
}
非常感谢你在这方面的帮助,蒂姆。
function myGetSelection(){
if(document.selection){ //IE
return document.selection.createRange().text;
} else{
return window.getSelection().toString();
}
}
相关文章:
- Angular js$Interval怪异行为-Firefox Chrome
- 由于传输层/meta/connect调用,使用faye/nodejs在Firefox / chrome上的初始页面加载时
- d3实现可以在firefox/chrome上运行,但不能在iPad上运行
- 为什么这个表达式在 Firefox/Chrome 中返回 true,而在 IE 中返回 false
- Jasmine 测试在测试运行中、Firefox/Chrome 之间以及检查器开/关时的结果不一致
- JavaScript XML解析在IE中工作,但在Firefox / Chrome中不起作用
- contentEditable and lastChild IE, Firefox & Chrome LastC
- WEBGL_draw_buffers最新的Firefox / Chrome上不支持
- 在Firefox / Chrome页面中打开IE浏览器
- 网站不适用于Firefox/Chrome,但适用于IE
- 任何使IE显示CSS3/HTML5与Firefox/Chrome相同的脚本
- onblur事件在IE/Firefox/Chrome中有所区别
- JQuery翻译函数在firefox/chrome中不起作用
- AudioContext Analyser在firefox/chrome上工作,但在Safari上不工作
- document.body.scrollHeight在firefox/chrome中产生两种不同的结果
- javascript数组的作用域不同于IE9和FireFox/Chrome
- 开发一个Firefox/Chrome扩展,覆盖形状的网页
- 如何隐藏地址栏在最新版本(Firefox, chrome和internet explorer)使用javascript窗
- innerHTML写上.value适用于IE9 IE8,但不适用于IE10, Firefox, Chrome
- Scrollbottom js不工作在Firefox, Chrome, Safari