IE 8 getSelection()。anchorOffset替代方案 [英] IE 8 getSelection().anchorOffset alternative

查看:763
本文介绍了IE 8 getSelection()。anchorOffset替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Javascript代码,适用于现代浏览器:

I have got a Javascript code, which works well for modern browsers:

var offset = getSelection().anchorOffset;
var node   = getSelection().anchorNode;

如何在IE 8上获得相同的结果,IE 8没有window.getSelection()方法?

How can I get the same result on IE 8, which does not have window.getSelection() method?

推荐答案

有一个解决方案。可以使用 Rangy 库来扩展浏览器的对象模型:

There is a solution. It is possible to extend browser's object model using Rangy library:

window.getSelection = rangy.getSelection



<由于Rangy可能需要一些时间来初始化,因为我们现在不需要这个用于现代浏览器,还有一些解决方法:

As it may take some time for Rangy to init and as we will not need this for modern browsers, some more workaround:

/* 
IE 8 getSelection() missing object and properties simple hack
*/
if (window.getSelection == undefined) {                /* IE? */
    var wgS = setInterval(function(){                  /* wait for Rangy */
        if (rangy.initialized) {
            window.getSelection = rangy.getSelection;  /* do the stuff */
            clearTimeout(wgS);                         /* exit */
        }
     }, 10);
}

这篇关于IE 8 getSelection()。anchorOffset替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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