[英]Javascript Keydown Not Triggering
I need help with getting the keydown of up arrow to trigger.我需要帮助来触发向上箭头的按键。 I dont really understand what the problem with this is, most likely my messy formatting, but the keydown just doesn't trigger.我真的不明白这是什么问题,很可能是我的格式混乱,但 keydown 只是没有触发。
<script> window.addEventListener('keyup', (event) => { if (event.key == 'ArrowUp') { close(); window.open("https://bsd.instructure.com/?login_success=1"); } }); </script>
Check out the window.close()
and window.open()
API.查看window.close()
和window.open()
API。
JavaScript cannot open windows if the browser blocks them.如果浏览器阻止,JavaScript 将无法打开窗口。 I tried your code and it says:我试过你的代码,它说:
"Firefox prevented this site from opening a pop-up window. [Options]". “Firefox 阻止此站点打开弹出窗口。[选项]”。
You can only open windows user-generated (eg button / link click).您只能打开用户生成的窗口(例如按钮/链接点击)。
Your event is working fine, but the browser is preventing the pop-up window.您的活动运行良好,但浏览器阻止了弹出窗口。
window.addEventListener('keyup', (event) => {
if (event.key == 'ArrowUp') {
window.close(); // Blocked for certain standards
window.open("https://bsd.instructure.com/?login_success=1"); // Blocked in the user's preferences
}
});
I also recommend you use document
events instead of window
:我还建议您使用document
事件而不是window
:
document.addEventListener('keyup', (event) => {
if (event.key == 'ArrowUp') {
// Action here
}
});
You can use the window location
property to navigate to pages.您可以使用窗口location
属性导航到页面。
document.addEventListener('keyup', (event) => { if (event.key == 'ArrowUp') { window.close(); window.location.replace("https://bsd.instructure.com/?login_success=1"); } });
Only for the sake of completeness.只是为了完整性。 as @Mike 'Pomax' Kamermans already answered the question correctly.因为@Mike 'Pomax' Kamermans 已经正确回答了这个问题。 bind the document object to the event instead of the window object.将文档对象绑定到事件而不是窗口对象。
document.addEventListener('keyup', (event) => { if (event.key == 'ArrowUp') { console.log('Arrow UP'); close(); } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.