Settings | Sign in | Sign up

The author has 2 posts.

Javascript控制文本框中的光標,支持ie,firefox等瀏覽器

Floor 1 巨大八爪鱼 9/14/14 10:08
/* Functions related to cursor */
var POS_END = -10;
function SetCursorPosition(name, beginning, end)
{
    var obj = $(name);
    if (typeof(beginning) == "undefined")
        beginning = 0;
    if (beginning == POS_END)
        beginning = obj.value.length;
    if (typeof(end) == "undefined")
        end = beginning;
    
    if (obj.setSelectionRange)
    {
        //For IE
        obj.focus();
        obj.setSelectionRange(beginning, end);
        return null;
    }
    else if (obj.createTextRange)
    {
        //For Firefox
        var range = obj.createTextRange();
        range.collapse(true);
        range.moveEnd('character', end);
        range.moveStart('character', beginning);
        range.select();
        return range;
    }
}
Floor 2 巨大八爪鱼 4/5/15 11:05
這個代碼不能控制可編輯html區域中的光標。

Content converter:

Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.
©2010-2025 Purasbar Ver3.0 [Mobile] [Desktop]
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported license.