Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

javascript - 如何防止退格键导航回来?(How can I prevent the backspace key from navigating back?)

On IE I can do this with the (terribly non-standard, but working) jQuery(在IE上,我可以使用(非常非标准但可以正常工作)jQuery来实现这一点)

if ($.browser.msie) $(document).keydown(function(e) { if (e.keyCode == 8) window.event.keyCode = 0;}); But is it possible to do in a way which works on Firefox, or in a cross-browser way for a bonus?(但有可能以一种适用于Firefox的方式,或以跨浏览器的方式获得奖金吗?) For the record:(作为记录:) $(document).keydown(function(e) { if (e.keyCode == 8) e.stopPropagation(); }); does nothing.(什么也没做。) $(document).keydown(function(e) { if (e.keyCode == 8) e.preventDefault(); }); solves the problem, but renders the backspace key unusable on the page, which is even worse than the original behaviour.(解决了问题,但是在页面上呈现退格键不可用,这比原始行为更糟糕。) EDIT: The reason I do this is that I'm not creating a simple web page but a large application.(编辑:我这样做的原因是我不是创建一个简单的网页,而是一个大型的应用程序。) It is incredibly annoying to lose 10 minutes of work just because you pressed backspace in the wrong place.(因为你在错误的地方按下退格键而失去10分钟的工作是令人难以置信的烦恼。) The ratio of preventing mistakes vs. annoying users should be way above 1000/1 by preventing the backspace key from navigating back.(通过防止退格键导航回来,防止错误与烦人用户的比例应该高于1000/1。) EDIT2: I'm not trying to prevent history navigation, just accidents.(编辑2:我不是想阻止历史导航,只是意外事故。) EDIT3: @brentonstrines comment (moved here since the question is so popular): This is a long-term 'fix', but you could throw your support behind the Chromium bug to change this behavior in webkit(EDIT3:@brentonstrines评论(因为这个问题非常受欢迎而搬到这里):这是一个长期的'修复',但你可以抛弃你的支持Chromium bug来改变webkit中的这种行为)   ask by erikkallen translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This code solves the problem, at least in IE and Firefox (haven't tested any other, but I give it a reasonable chance of working if the problem even exists in other browsers).(这个代码解决了这个问题,至少在IE和Firefox中(没有测试任何其他的,但如果问题甚至存在于其他浏览器中,我给它一个合理的工作机会)。)

// Prevent the backspace key from navigating back. $(document).unbind('keydown').bind('keydown', function (event) { if (event.keyCode === 8) { var doPrevent = true; var types = ["text", "password", "file", "search", "email", "number", "date", "color", "datetime", "datetime-local", "month", "range", "search", "tel", "time", "url", "week"]; var d = $(event.srcElement || event.target); var disabled = d.prop("readonly") || d.prop("disabled"); if (!disabled) { if (d[0].isContentEditable) { doPrevent = false; } else if (d.is("input")) { var type = d.attr("type"); if (type) { type = type.toLowerCase(); } if (types.indexOf(type) > -1) { doPrevent = false; } } else if (d.is("textarea")) { doPrevent = false; } } if (doPrevent) { event.preventDefault(); return false; } } });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...