javascript,模拟键盘事件,使用chrome(webkit) [英] javascript, simulate keyboard events, work on chrome(webkit)
问题描述
if(keyCount == lineLimit){
// FF中的方法,不需要
var mock = document.createEvent(KeyboardEvent); //或KeysEvent
mock.initKeyEvent(keypress,true,true,null,false,false,false,false,14,0);
var x = document.getElementById('InputCategory');
//在输入
$(this).height(div_height + font_height + offset_height)之前上升高度;
//模拟输入
x.dispatchEvent(mock);
// init keyCount
keyCount = 0;
}
它可以工作,但在基于webkit的浏览器(如chrome)上无法生效。
所以我问谷歌,发现键盘事件是DOM Level 3事件之一,这里是一个aticle: http://www.w3.org/TR/DOM-Level-3-Events/
然后我知道/ * initKeyboardEvent /在chrome& safari,/ / initUIEvent * /我试过了,它也没有工作。 虚拟键盘事件reall可以在chrome上模拟吗? plesase帮助我:) 这很有效,但它不会生成按键事件,而是生成文本插入事件。 / p> 在输入末尾插入'test'用 in FF, i've used this code: it works, but could not be effective on webkit-based browsers like chrome. so i asked google and found keyboard event is one of the DOM Level 3 events,here is an aticle: http://www.w3.org/TR/DOM-Level-3-Events/ then i knew /* initKeyboardEvent / is not supported on chrome & safari, / initUIEvent */ i've tried, it didn't work also. Do virtual keyboard events reall can be simulated on chrome ? plesase help me :) This works, but it's not generating a keypress-event, rather a text-insert event. That inserts the word 'test' at the end of the input (in your case you'd probably replace that by 这篇关于javascript,模拟键盘事件,使用chrome(webkit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
var te = document.createEvent('TextEvent');
te.initTextEvent('textInput',true,true,window,'test');
< element> .dispatchEvent(te);
\\\
。
替换
if (keyCount == lineLimit) {
// method in FF, no Chrome
var mock = document.createEvent("KeyboardEvent"); // or KeysEvent
mock.initKeyEvent("keypress",true,true,null,false,false,false,false,14,0);
var x = document.getElementById('InputCategory');
// rise height before Enter
$(this).height(div_height + font_height + offset_height);
// mock Enter
x.dispatchEvent(mock);
// init keyCount
keyCount = 0;
}
var te = document.createEvent('TextEvent');
te.initTextEvent('textInput', true, true, window, 'test');
<element>.dispatchEvent(te);
\n
.