我有一个按钮和以下 javascript 例程.
I have a button and the following javascript routine.
$("button").keydown( function(key) {
switch(key.keyCode) {
case 32: //space
return false;
}
} );
据我了解,return false; 会阻止按键被处理.所以 $("button").click(); 不会被调用.对于其他键码,这按预期工作.例如,如果我截取40,即向下按钮,则页面不滚动.
as I understood it, the return false; would stop the keypress from being processed. So $("button").click(); would not be called. For other keyCodes, this works as expected. For example, if I intercept40, which is the down button, the page is not scrolling.
我在 Firefox 中注意到了这种行为.
I noticed this behavior in Firefox.
为什么 return false; 不会停止空间上的按钮单击事件?javascript 规范对此有何评论?
Why does the return false; does not stop the button click event on space? What does the javascript spec say about this?
希望这能回答你的问题:
Hope this answers your question:
<input type="button" value="Press" onkeydown="doOtherStuff(); return false;">
return false; 如果在 HTML 中的事件处理程序属性的末尾调用,则成功取消跨浏览器的事件.据我所知,这种行为在任何地方都没有正式规定.
return false; successfully cancels an event across browsers if called at the end of an event handler attribute in the HTML. This behaviour is not formally specified anywhere as far as I know.
如果您改为通过 DOM 元素上的事件处理程序属性设置事件(例如 button.onkeydown = function(evt) {...})或使用 addEventListener/attachEvent(例如 button.addEventListener("keydown", function(evt) {...}, false))然后只返回 false 来自该函数的功能并非在每个浏览器中都有效,您需要从我的其他答案中执行 returnValue 和 preventDefault() 内容.preventDefault 在 DOM 2 规范 并由大多数主流现代浏览器实现.returnValue 是特定于 IE 的.
If you instead set an event via an event handler property on the DOM element (e.g. button.onkeydown = function(evt) {...}) or using addEventListener/attachEvent (e.g. button.addEventListener("keydown", function(evt) {...}, false)) then just returning false from that function does not work in every browser and you need to do the returnValue and preventDefault() stuff from my other answer. preventDefault is specified in the DOM 2 spec and is implemented by most mainstream modern browsers. returnValue is IE-specific.
这篇关于为什么在 keydown 回调中返回 false 不会停止按钮单击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 Angular 2/Typescript 中使用 IScrollUse IScroll in Angular 2 / Typescript(在 Angular 2/Typescript 中使用 IScroll)
Anime.js 在 Ionic 3 项目中不起作用anime.js not working in Ionic 3 project(Anime.js 在 Ionic 3 项目中不起作用)
Ionic 3 - 使用异步数据更新 ObservableIonic 3 - Update Observable with Asynchronous Data(Ionic 3 - 使用异步数据更新 Observable)
Angular 2:在本地 .json 文件中找不到文件Angular 2: file not found on local .json file(Angular 2:在本地 .json 文件中找不到文件)
在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指In Ionic 2, how do I create a custom directive that uses Ionic components?(在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指令?)
将 ViewChild 用于动态元素 - Angular 2 &离子2Use ViewChild for dynamic elements - Angular 2 amp; ionic 2(将 ViewChild 用于动态元素 - Angular 2 amp;离子2)