如何使containsNode()在IE中工作?

时间:2015-12-04 06:39:58

标签: javascript internet-explorer

我有5个寄生虫,我想确认选择了哪个寄生虫。

<p>The 1st paragraph. Hello everyone.Nice meeting you!</p>
<p>The 2nd paragraph. Hello everyone.Nice meeting you!</p>
<p>The 3rd paragraph. Hello everyone.Nice meeting you!</p>
<p>The 4th paragraph. Hello everyone.Nice meeting you!</p>
<p>The 5th paragraph. Hello everyone.Nice meeting you!</p>

JS代码如下:

var para = document.getElementsByTagName('p');
    var result = [];

    document.onmouseup = function(){
        var selectText = window.getSelection();//get the selected range obj,ie9+ support getSelection()
        if(selectText.toString()){
            for (var i = 0;i<para.length;i++){
                if(selectText.containsNode(para[i],true)){
                    result.push(i+1);//get sequence number of the selected paras
                }
            }
            alert(result);
            result = [];
        }
    }   

当我选择这样的文字时:

Snapshot

窗口警报&#34; 1,2,3&#34;,这意味着我选择了第1,第2和第3段。

但是这在IE中不起作用。我搜索了信息,并且知道IE不支持方法containsNode()。如何使containsNode()在IE中工作或使用其他一些方法来获得相同的结果?

PS:选择以下文字时:

snapshot2

为什么它警告&#34; 1,2&#34; ?

1 个答案:

答案 0 :(得分:0)

它应该在IE9 +中工作,下面的低版本是链接

IE 8 getSelection().anchorOffset alternative” 你可以帮忙