IE 8 getSelection().anchorOffset alternative

前端 未结 1 1409
情歌与酒
情歌与酒 2021-01-28 01:32

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

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


        
1条回答
  •  余生分开走
    2021-01-28 02:15

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

    0 讨论(0)
提交回复
热议问题