javascript - Javascript Keydown 未触发
问题描述
我需要帮助来触发向上箭头的按键。我真的不明白这是什么问题,很可能是我凌乱的格式,但是keydown不会触发。
<script>
window.addEventListener('keyup', (event) => {
if (event.key == 'ArrowUp') {
close();
window.open("https://bsd.instructure.com/?login_success=1");
}
});
</script>
解决方案
查看window.close()
和window.open()
API。
如果浏览器阻止它们,JavaScript 将无法打开它们。我尝试了您的代码,它显示:
“Firefox 阻止此站点打开弹出窗口。[选项]”。
您只能打开用户生成的窗口(例如按钮/链接点击)。
您的活动运行良好,但浏览器阻止了弹出窗口。
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
}
});
我还建议您使用document
事件而不是window
:
document.addEventListener('keyup', (event) => {
if (event.key == 'ArrowUp') {
// Action here
}
});
您可以使用 windowlocation
属性导航到页面。
document.addEventListener('keyup', (event) => {
if (event.key == 'ArrowUp') {
window.close();
window.location.replace("https://bsd.instructure.com/?login_success=1");
}
});
推荐阅读
- python - JSON解码不适用于Python请求
- python - 防止在使用数据着色器重新渲染图像时重新缩放颜色图
- javascript - Javascript decodeURICompnent() 不适用于 MySQL 编码的 UTF-8 字符串
- node.js - 使用 AWS Amplify 创建 process.env 变量?
- bash - 显示服务信息后退出命令无法退出 bash shell?
- android - “在 API 级别 X 中添加”的 Android 文档含义
- javascript - 路由解析器中的 Angular 8 地图数据
- javascript - 为什么 HasMany Associate 进行 UPDATE 查询
- python - 用条件连接 csv 文件
- java - 如何在输出中转换长数据类型以使代码正常工作?