当我使用登录框时,如何防止退格键将我带回到上一页

发布于 2024-09-07 14:50:10 字数 234 浏览 5 评论 0原文

登录框位于网站的标题中,所有页面都可用。 我正在使用 JavaScript:

<script type="text/javascript" language="javascript">
       history.forward();

这解决了登录框中退格的问题。 但是,当我浏览网站的其余部分时,我可以使用退格键查看上一页,而我只能使用导航菜单。我该如何解决这个问题?

the login box is in the header of the website, availalbe on all pages.
I'm using a javascript :

<script type="text/javascript" language="javascript">
       history.forward();

Which solves the issue of the backspace in the login box.
But while I'm browsing the rest of the website, I can use backspace to see the previous page, I'd have to use the navigation menu only. How can I solve that issue ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫腻 2024-09-14 14:50:10

试试这个 JavaScript

<script for="document" event=onkeydown>

// Check if this is a Backspace
 if (window.event.keyCode == 8)
 {
    // Cancel backspace if not in a text element
    if(document.activeElement.type != "text")
    {
        window.event.returnValue=false;
    }
 }
 </script>

Try this JavaScript

<script for="document" event=onkeydown>

// Check if this is a Backspace
 if (window.event.keyCode == 8)
 {
    // Cancel backspace if not in a text element
    if(document.activeElement.type != "text")
    {
        window.event.returnValue=false;
    }
 }
 </script>
旧故 2024-09-14 14:50:10

网页更改浏览器的按键绑定是一个有争议的话题,因为这违反了最小惊喜原则(用户体验设计师应该熟悉这一原则)。另外,并非所有浏览器都有此键映射(Bksp -> 返回)。

也就是说:您可以尝试捕获关键事件,如所述 此处 并取消事件传播。不确定这是否适用于所有键,因此您必须尝试看看。

A webpage changing the key bindings of the browser is a controversial topic, as this violates the principle of the least surprise (which should be familiar to an UX designer). Also, not all browsers have this key mapping (Bksp -> Go back).

That said: you can try capturing the key events, as described e.g. here and cancelling the event propagation. Not sure if this is possible for all keys, so you'd have to try and see.

So尛奶瓶 2024-09-14 14:50:10

如果您使用正确的 HTML 文本输入框并且焦点位于其上,则退格键不会让用户返回。如果您没有使用正确的 HTML 输入框,那么我建议您这样做。浏览器会为你处理这些事情。

If you're using a proper HTML text input box and the focus is on it then backspace will not take the user back. If you're not using a proper HTML input box then I suggest you do so. Browsers handle this stuff for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文