@Nick Craver,出于几个原因的可耻回答。回答这个问题,或者至少深思熟虑地光顾。
这是一个基于原型的解决方案,我最终将其用于我的表单,因为用户抱怨退格键会将它们从表单中带走(这显然是违反直觉的事情,有人想知道为什么所有浏览器都使用退格键作为后退按钮)。
// event handlers must be attached after the DOM is completely loaded
Event.observe(window, 'load', function() {
// keypress won't fire for backspace so we observe 'keydown'
Event.observe(window, 'keydown', function(event){
// 8 == backspace
if( event.keyCode == 8) {
// with no field focused, the target will be HTMLBodyElement
if( event.target == document.body) {
// stop this event from propagating further which prevents
// the browser from doing the 'back' action
event.stop();
}
}
});
});