开发者

Jquery Event Trigger Keyboard Simulation

i'm doing a minimal example http://jsfiddle.net/PSYCKIC/SQZVH/1/

Can't not put it work, but the idea is to simulate a keyboard event as keypress,keydown 开发者_开发百科or keyup that works for every browser(Firefox, Safari, Chrome , IE).

Not sure why the code isn't working, any ideia how can i do it?


This is the current solution working for Chrome and Firefox, still need to check if work Opera and IE

// put cursor and input text in correct position

function setCaretPosition(elem, caretPos) {
    if (elem != null) {
        if (elem.createTextRange) {
            var range = elem.createTextRange();
            range.move('character', caretPos);
            range.select();
        }
        else {

            if (elem.selectionStart) {
                elem.setSelectionRange(caretPos, caretPos);
                elem.focus();

                // Workaround for FireFox overflow no scroll problem
                if ($.browser.mozilla) {

                    // Trigger a "space" keypress.
                    var evt = document.createEvent("KeyboardEvent");
                    evt.initKeyEvent("keypress", true, true, null, false, false, false, false, 0, 32);
                    elem.dispatchEvent(evt);

                }
                // for webkit 
                if ($.browser.webkit || $.browser.msie) {
                    var te = document.createEvent('TextEvent');
                    // Trigger a "space" keypress.
                    te.initTextEvent('textInput', true, true, window, ' ');
                    elem.dispatchEvent(te);
                }

            }
            else
                elem.focus();
        }
    }
}


$("#test").trigger(event.type);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜