【问题标题】:IE 8 getSelection().anchorOffset alternativeIE 8 getSelection().anchorOffset 替代
【发布时间】:2013-09-18 11:36:09
【问题描述】:

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

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

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

【问题讨论】:

  • 我会改写:我需要在 IE 8 中获得 carret 位置和选择的锚节点:) 他们如何在 IE

标签: javascript internet-explorer-8 getselection


【解决方案1】:

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

window.getSelection = rangy.getSelection

由于 Rangy 可能需要一些时间来初始化,而且我们在现代浏览器中不需要它,所以还有一些解决方法:

/* 
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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 2012-11-07
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多