如何在textarea获得插入位置

时间:2014-03-22 10:27:03

标签: javascript

JS

function doGetCaretPosition (ctrl)
{
    var CaretPos = 0;
    // IE Support
    if (document.selection)
    {
        ctrl.focus ();
        var Sel = document.selection.createRange ();
        Sel.moveStart ('character', -ctrl.value.length);
        CaretPos = Sel.text.length;
    }
    // Firefox support
    else if (ctrl.selectionStart || ctrl.selectionStart == '0')
    {
        CaretPos = ctrl.selectionStart;
    }
    return (CaretPos);
}

window.onload=function(){       
    editor = CKEDITOR.replace('content');
}

HTML:

<body>
    <form name="inForm" method="post">

        <textarea id="content" name="content" cols="100%" rows="10">1234dfgdf5</textarea>
        <br>
        <input type="button" onclick="alert(doGetCaretPosition(document.getElementById('content')));"
            value="Get Position">
    </form>
</body>

为什么&#34;获得职位&#34;然后点击它继续零来了?

0 个答案:

没有答案