插入位置/选择DIV,Textbox,Textarea等

时间:2012-10-18 13:24:30

标签: javascript internet-explorer firefox selection caret

是否有完整的解决方案可以在不同元素的每个浏览器中获取插入位置和/或选择。我正在寻找一个我可以执行的解决方案 mGetCaretPosition(iControl)将返回其内部元素的插入位置
我尝试了很多功能:

  • selection (window/document) [document=IE, window=Opera]
  • getSelection (window/document) [document=Firefox, document=Chrome, document=Safari]
  • selectionStart (input/textarea) [All]
  • craeteRange (selection)
  • createTextRange (selection)


调用document.selection.createRange()。text这样的方法不会返回插入位置,因为它没有选择。设置tRange.moveStart('character', - X)时,X不是已知值。当你在div中使用它并且插入符号在中间时它会在div之前获取代码。

1 个答案:

答案 0 :(得分:3)

我今天已经建立了这个。这是你回应亚历克斯和谷歌内部其他结果的组合。我已经在PC上的浏览器IE9,Chrome,Opera,Safari和Firefox中进行了测试,并在Android上使用默认浏览器,Firefox,Chrome和Opera进行了HTC Sensation测试。

只有移动设备上的Opera确实存在一些问题。

我的解决方案:

// Control
var BSControl = function(iControl)
{
    // Variable
    var tControl = (typeof iControl == 'string' ? document.getElementById(iControl) : iControl);

    // Get Caret
    this.mGetCaret = function()
    {
        // Resultaat aanmaken
        var tResult = -1;

        // SelectionStart
        // *) Input & Textarea
        if(tResult == -1 && (tControl.selectionStart || tControl.selectionStart == '0'))
        {
            tResult = tControl.selectionStart;
        }

        // ContentWindow.GetSelection
        // *) IFrame
        if(tResult == -1 && (tControl.contentWindow && tControl.contentWindow.getSelection))
        {
            var tRange= tControl.contentWindow.getSelection().getRangeAt(0); 
            tResult = tRange.startOffset;
        }

        // GetSelection
        // *) Div
        if(tResult == -1 && (window.getSelection))
        {
            var tRange= window.getSelection().getRangeAt(0); 
            tResult = tRange.startOffset;
        }

        // Resultaat teruggeven
        return tResult;
    }

    // Set Caret
    this.mSetCaret = function(iPosition)
    {
        // SelectionStart
        // *) Input & Textarea
        if(tControl.selectionStart || tControl.selectionStart == '0')
        {
            tControl.selectionStart = iPosition;
            tControl.selectionEnd = iPosition;
            return;
        }

        // ContentWindow.GetSelection
        // *) IFrame
        if(tControl.contentWindow && tControl.contentWindow.getSelection)
        {
            var tRange = tControl.contentDocument.createRange();
            tRange.setStart(tControl.contentDocument.body.firstChild, iPosition);
            tRange.setEnd(tControl.contentDocument.body.firstChild, iPosition);

            var tSelection = tControl.contentWindow.getSelection();
            tSelection.removeAllRanges();
            tSelection.addRange(tRange);

            return;
        }

        // GetSelection
        // *) Div
        if(window.getSelection)
        {
            var tSelection = window.getSelection();
            var tRange= tSelection.getRangeAt(0); 

            tRange.setStart(tControl.firstChild, iPosition);
            tRange.setEnd(tControl.firstChild, iPosition);

            tSelection.removeAllRanges();
            tSelection.addRange(tRange);

            return;
        }
    }

    // Get Selection
    this.mGetSelection = function()
    {
        // Resultaat aanmaken
        var tResult = null;

        // SelectionStart
        // *) Input & Textarea
        if(tResult == null && (tControl.selectionStart || tControl.selectionStart == '0'))
        {
            tResult = this.mGet().substring(tControl.selectionStart, tControl.selectionEnd);
        }

        // ContentWindow.GetSelection
        // *) IFrame
        if(tResult == null && (tControl.contentWindow && tControl.contentWindow.getSelection))
        {
            var tSelection = tControl.contentWindow.getSelection() 
            tResult = tSelection.toString();
        }

        // GetSelection
        // *) Div
        if(tResult == null && (window.getSelection))
        {
            var tSelection = window.getSelection() 
            tResult = tSelection.toString();
        }

        // Resultaat teruggeven
        return tResult;
    }

    // Set Selection
    this.mSetSelection = function(iFrom, iUntil)
    {
        // SelectionStart
        // *) Input & Textarea
        if(tControl.selectionStart || tControl.selectionStart == '0')
        {
            tControl.selectionStart = iFrom;
            tControl.selectionEnd = iUntil;
            return;
        }

        // ContentWindow.GetSelection
        // *) IFrame
        if(tControl.contentWindow && tControl.contentWindow.getSelection)
        {
            var tRange = tControl.contentDocument.createRange();
            tRange.setStart(tControl.contentDocument.body.firstChild, iFrom);
            tRange.setEnd(tControl.contentDocument.body.firstChild, iUntil);

            var tSelection = tControl.contentWindow.getSelection();
            tSelection.removeAllRanges();
            tSelection.addRange(tRange);

            return;
        }

        // GetSelection
        // *) Div
        if(window.getSelection)
        {
            var tSelection = window.getSelection();
            var tRange= tSelection.getRangeAt(0); 

            tRange.setStart(tControl.firstChild, iFrom);
            tRange.setEnd(tControl.firstChild, iUntil);

            tSelection.removeAllRanges();
            tSelection.addRange(tRange);

            return;
        }
    }

    // Set
    this.mSet = function(iValue)
    {
        // Afhankelijk van aanwezige property waarde instellen
        if('value' in tControl)
        {
            tControl.value = iValue;
        }else if('innerText' in tControl)
        {
            tControl.innerText = iValue;
        }else if('textContent' in tControl)
        {
            tControl.textContent = iValue;
        }else if('innerHTML' in tControl)
        {
            tControl.innerHTML = iValue;
        }
    }

    // Get
    this.mGet = function()
    {
        // Resultaat aanmaken
        var tResult = null;

        // Afhankelijk van aanwezige property waarde instellen
        if('value' in tControl)
        {
            tResult = tControl.value;
        }else if('innerText' in tControl)
        {
            tResult = tControl.innerText;
        }else if('textContent' in tControl)
        {
            tResult = tControl.textContent;
        }else if('innerHTML' in tControl)
        {
            tResult = tControl.innerHTML;
        }

        // Resultaat teruggeven
        return tResult;
    }
}