gpt4 book ai didi

javascript - 在 JavaScript 中模拟输入

转载 作者:太空狗 更新时间:2023-10-29 16:45:57 25 4
gpt4 key购买 nike

我尝试在特定的 TextArea 中用 JavaScript 模拟 Enter。这是我的代码:

 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 id="text"> </textarea>

当我调用 enter1() 时,它不会在 TextArea 中执行任何操作。这是为什么?

最佳答案

对于调度事件有问题的任何人,请尝试添加键值对 bubbles: true:

const keyboardEvent = new KeyboardEvent('keydown', {
code: 'Enter',
key: 'Enter',
charCode: 13,
keyCode: 13,
view: window,
bubbles: true
});

关于javascript - 在 JavaScript 中模拟输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30003774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com