【发布时间】:2020-10-07 09:28:31
【问题描述】:
我想在不使用 jQuery 的情况下触发按键事件作为对事件侦听器的反应
let arrow = document.querySelector("#arrow");
//When you click on the arrow
arrow.addEventListener('click', function(e){
// It triggers a keypress (down)
$(document).trigger(keypressing);
});
[编辑] 我试过了,但似乎没有触发模拟按键:
let arrow = document.querySelector(".scroll-down");
arrow.addEventListener('click', function(e){
console.log('simulating the keypress of down arrow')
document.dispatchEvent(new KeyboardEvent('keypress', {'key': 'x'}));
});
$(window).bind('keypress', function(event) {
if (event.key == 'x') {
console.log('its working with x')
}
});
【问题讨论】:
-
使用.click()
-
你的
#arrow元素是什么?输入 ?按钮? -
是一个div容器,包裹了3个span
标签: javascript dom-events keypress