本文介绍了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?

推荐答案

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

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

window.getSelection = rangy.getSelection



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替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 01:43