- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
一旦获得焦点,我需要拦截 Tabbable DIV 上的 Enter 或 Space(13 或 32) 按键。
我看到以下内容:
Chrome 两者都支持。
Firefox 不支持空格。
IE11不支持Enter。
可以依次用每个浏览器打开以下代码片段以查看差异:
$('#buttondiv').keypress(function (e) {
// First log which key was pressed
console.log(e.keyCode);
if (e.keyCode == 13 || e.keyCode == 32) {
alert('Intercepted Enter or Space');
}
});
#buttondiv {
border: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<button id="buttonfocus" onclick="$('#buttondiv').focus()">
Click here to focus DIV first
</button>
<div id="buttondiv" tabindex="0">
Press Enter or Space on DIV after focusing
</div>
此外,如果有兴趣,JSFiddle(但 JSFiddle 不会在 IE 中打开): http://jsfiddle.net/v406agb1/
最佳答案
您应该使用keydown
而不是keypress
。这就是official documentation says :
Note: as the keypress event isn't covered by any official specification, the actual behavior encountered when using it may differ across browsers, browser versions, and platforms.
This method is a shortcut for .on( "keypress", handler ) in the first two variations, and .trigger( "keypress" ) in the third.
The keypress event is sent to an element when the browser registers keyboard input. This is similar to the keydown event, except that modifier and non-printing keys such as Shift, Esc, and delete trigger keydown events but not keypress events. Other differences between the two events may arise depending on platform and browser.
$('#buttondiv').keydown(function (e) {
// First log which key was pressed
console.log(e.keyCode);
if (e.keyCode == 13 || e.keyCode == 32) {
alert('Intercepted Enter or Space');
}
});
#buttondiv {
border: 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<button id="buttonfocus" onclick="$('#buttondiv').focus()">
Click here to focus DIV first
</button>
<div id="buttondiv" tabindex="0">
Press Enter or Space on DIV after focusing
</div>
关于javascript - Tabbable DIV 上的空格键或 Enter 键 : Some browsers don't intercept Keypress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54410536/
我有一个注入(inject)了 Ninject 3.0 的服务类。我已经设置了它,因此它的代理是类代理而不是接口(interface)代理。该服务有 2 个方法,第一个方法返回广泛结果,第二个方法调用
当工具提示即将打开时,我想在状态栏中显示 wpf 应用程序中任何控件的工具提示文本。 当然,我可以尝试递归遍历主窗口的所有子控件,然后 设置他们的 ToolTipOpening事件总是相同的方法。但是
给定一个打开到 Web 服务器的套接字连接的 Flash 应用程序,是否可以读取与 Java 应用程序交换的数据包,而无需重定向所有 Flash 流量(即无需编写套接字代理)? 最佳答案 您想要做的事
我有一个文本框,我想对其进行一些验证。目前我有这段代码: function updateChanger() { // Validate input var likeMessage =
Linux 中的一些命令,例如。 ssh-agent $BASH 确保当前控制台中运行的命令的某些操作被另一个工具“拦截”。这一般是如何运作的?为了理解这一点,我应该研究哪些主题?假设我想制作一个工具
拦截过滤器模式(Intercepting Filter Pattern)用于对应用程序的请求或响应做一些预处理/后处理 定义过滤器,并在把请求传给实际目标应用程序之前应用在请求上 过滤器可以做认证
我一直在尝试实现应用程序范围的拦截器,但是,我一直看到以下错误: TypeError: this.interceptor.intercept is not a function 经过多次调试,我意识到
众所周知,我们可以像这样配置拦截器: 我的问题,如何配置
我正在开发一款游戏,里面有寻的导弹。目前,它们只是转向目标,这会产生看起来相当愚蠢的结果,所有导弹都跟随目标周围。 我想创造一种更致命的导弹,它将瞄准目标“将”到达的地方,但我对如何做到这一点感到有点
从 iTunes 中捕获事件有什么好的解决方案吗? 我想构建一个位于 iTunes 内部的插件,并响应特定歌曲的评级变化。对于 Windows,他们有一个我还没有深入研究的 SDK,但我也想为 Mac
现在我有一个名为 chartModelService 的 Angular 服务,上面有这个方法: this.fetchChartModel = function() { return $htt
我的 Controller 有以下方法: @RequestMapping(method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED
如果您安装了 Facebook 应用程序,并使用应用程序 openURL 方法打开 Facebook 页面,您会注意到 Facebook 应用程序接管并处理此 URL,而不是 Safari 执行此操作
我有这样一个类: class A { public static void makeCall() { URL url = "www.google.com"; I
我希望我的应用拦截手机发现的所有标签。这样我想检查标签的 uid 并在之后传递 Intent (显示应用程序选择器等)。 这就是我目前在 list 中的内容,以及用于 TAG_DISCOVERE
我正在用 Playwright's API testing 测试服务器. 我有一个简单的 GET 路由,它总是返回 405 not allowed。它还记录调用了 API 路由。为了这个 StackO
有什么方法可以拦截 Android WebView 中的重定向请求? shouldInterceptRequest(WebView,WebResourceRequest) 似乎没有被调用! 我想通过以
This question already has answers here: Element MyElement is not clickable at point (x, y)… Other el
我有一个拦截器,捕获404错误 拦截(要求:HttpRequest,下一个:HttpHandler):可观察> return next.handle(req).pipe(retry(1),
有没有办法让 Cypress 捕获任何未使用 cy.intercept 处理和 stub 的请求。我希望 Cypress 返回一个有用的错误,以突出显示发出未 stub 请求的实例。目前它只是让这些请
我是一名优秀的程序员,十分优秀!