在IE中选择StartContainer

时间:2009-08-03 16:38:26

标签: internet-explorer selection range

我有一个选择对象,在IE中,我运行

range = selection.createRange();

然后我尝试获取startContainer,但我无法弄清楚如何。所有示例都显示我设置了startContainer,但我的目的是突出显示文本。如何在不先获取它的情况下知道将其设置为哪个元素?

我知道在FireFox中它就像

一样简单
range     = selection.getRangeAt(0);
firstNode = range.startContainer;

3 个答案:

答案 0 :(得分:2)

[已经两年了。] 好的,这将是丑陋的,但我想我得到了这个问题的答案(似乎在IE8上工作)。代码如下(解释在注释中):

/**
 * @param {window object} win The window from which the selection is to be retrieved. This could also be an iframe.contentWindow.
 */
function getRangeObject(win) { //Gets the first range object. 
    win=win || window;
    if (win.getSelection) { // W3C/FF/Chrome/Safari/Opera/IE9
        return win.getSelection().getRangeAt(0);    //W3C DOM Range Object
    }
    else if(win.document.selection) { // IE8
        return win.document.selection.createRange(); //Microsoft TextRange Object
    }
    return null;
}

function getStartContainer(win) {
win=win || window;
    var range=getRangeObject(win);
    if(range) {
        if(range.startContainer) { // W3C/FF/Chrome/Safari/Opera/IE9
            return range.startContainer;
        } else if(document.selection) { //IE8
            var rangeCopy=range.duplicate(); //Create a copy
            var rangeObj=range.duplicate();

            rangeCopy.collapse(true); //Go to beginning of the selection
        rangeCopy.moveEnd('character',1); //Select only the first character
            //Debug Message
            //alert(rangeCopy.text); //Should be the first character of the selection
            var parentElement=rangeCopy.parentElement();
            rangeObj.moveToElementText(parentElement); //Select all text of parentElement
            rangeObj.setEndPoint('EndToEnd',rangeCopy); //Set end point to the first character of the 'real' selection
            var text=rangeObj.text; //Now we get all text from parentElement's first character upto the real selection's first character

            //Iterate through all the child text nodes and check for matches
            //As we go through each text node keep removing the text value (substring) from the beginning of the text variable.
            var container=null;
            for(var node=parentElement.firstChild; node; node=node.nextSibling) {
                if(node.nodeType==3) {//Text node
                    var find=node.nodeValue;
                    var pos=text.indexOf(find);
                    if(pos==0 && text!=find) { //text==find is a special case
                        text=text.substring(find.length);
                    } else {
                        container=node;
                        break;
                    }
                }
            }
            range.startContainer=container; //Finally we are here
            //Debug Message
            //alert(container.nodeValue);
        }
    }
}

对于过多的评论感到抱歉(我认为这是必要的)。一旦你删除所有这些评论,你就会得到代码......不那么难看:P。

答案 1 :(得分:0)

我刚遇到这个问题。我找不到一个优雅的解决方案,所以这里是一个不优雅的解决方案:

注意:只有起始容器具有id

时才会起作用

首先检查浏览器是否为IE。然后,

            // var range should be your TextRange object
            var matches = range.htmlText.match(/<[^>]* id=([^\s]+)[^>]*>/);
            if (matches) {
                var startContainer = document.getElementById(matches[1]);
                if (startContainer) {
                    ...
                }
            }

以下是范围的方法:http://msdn.microsoft.com/en-us/library/ms535872(v=vs.85).aspx

我认为没有办法获得一套实际的dom objs。

答案 2 :(得分:-1)

我也在做类似的事情。我还没有进入IE部分,但我会建议迭代范围的属性列表,看看你有什么可用

var msg = '';
for (var i in range) {
    msg += i + ': ' + range[i] + '\n';
}
alert(msg);