我試圖模擬在JavaScript
在一個特定TextArea
。 代碼:模擬輸入JavaScript
function enter1() {
var keyboardEvent = document.createEvent('KeyboardEvent');
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? 'initKeyboardEvent' : 'initKeyEvent';
keyboardEvent[initMethod]('keydown', // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
13, // keyCodeArg : unsigned long the virtual key code, else 0
13 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
document.getElementById('text').dispatchEvent(keyboardEvent);
}
TextArea
:
<textarea id="text"> </textarea>
當我打電話ENTER1()什麼都不幹的textarea的。爲什麼?
[這裏](http://stackoverflow.com/questions/961532/firing-a-keyboard-event-in-javascript),[這裏](http://stackoverflow.com/questions/10455626/keydown-simulation-in-chrome-fires-normally-but-the-the-correct-key) – AvrilAlejandro
@avrilalejandro感謝艾薇兒的支持,但是每個功能都沒有通過「回車」。我不明白。 –