Javascript 输入类型 - 后退按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4629606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:20:33  来源:igfitidea点击:

input type - back button?

javascripthtml

提问by tony noriega

What is the best method (cross browser) to utilize an input type button, as a browser back button?

使用输入类型按钮作为浏览器后退按钮的最佳方法(跨浏览器)是什么?

回答by Mech Software

Are you looking to do something like this?

你想做这样的事情吗?

<input type="button" onclick="history.back();" value="Back">

You could also use

你也可以使用

<input type="button" onclick="history.go(-1);" value="Back">

回答by zzzzBov

"Best" is a little subjective.

“最好”有点主观。

@Mech Software has an acceptable solution, as it's a simple function call. If you're only making one back button, it's probably the way to go. However, I try to place code where it belongs:

@Mech Software 有一个可接受的解决方案,因为它是一个简单的函数调用。如果您只制作一个后退按钮,这可能是要走的路。但是,我尝试将代码放在它所属的位置:

HTML belongs in .html, CSS belongs in .css, and JavaScript belongs in .js files. Instead of giving the button an onclickattribute, I'd give it a class of back-buttonand attach the clicklistener to each .back-buttonelement.

HTML 属于 .html,CSS 属于 .css,而 JavaScript 属于 .js 文件。onclick我没有给按钮一个属性,而是给它一个类back-button并将click侦听器附加到每个.back-button元素。

HTML:HTML:
<input type="button" class="back-button" value="Back" />
<!-- or -->
<button type="button" class="back-button">Back</button>
JS:JS:
jQuery(function($){
  $('.back-button').click(function(e){
    history.back();
  });
});

I'm assuming the jQuery library, as it's relatively ubiquitous (let me know if you want the non-jQuery version of this).

我假设使用 jQuery 库,因为它相对普遍(如果您想要非 jQuery 版本,请告诉我)。

The reason I'd suggest this approach is that it's expandable. If you decide to do something more with your back buttons, or if browsers change (which happens all the time) it's simple to change the functionality in exactly one location, rather than having to hunt down every instance of onclick="history.back()".

我建议这种方法的原因是它是可扩展的。如果您决定用后退按钮做更多事情,或者如果浏览器发生变化(这种情况一直发生),只需在一个位置更改功能就很简单,而不必寻找onclick="history.back()".

回答by asdf

<input type="button" onclick="history.back();" value="Back">

The reason I'd suggest this approach is that it's expandable. If you decide to do something more with your back buttons, or if browsers change (which happens all the time) it's simple to change the functionality in exactly one location, rather than having to hunt down every instance of onclick="history.back()".

我建议这种方法的原因是它是可扩展的。如果您决定用后退按钮做更多事情,或者如果浏览器发生变化(这种情况一直发生),只需在一个位置更改功能就很简单,而不必寻找onclick="history.back()".

回答by slacker

Try This it works really well and looks neat too.

试试这个它真的很好用,看起来也很整洁。

  <center>
<script>
function goBack()
  {
  window.history.back()
  }
</script>
Wrong Link? <input type="button" value="Go Back" onclick="goBack()"/>
</center>

If you dont want it to say Wrong Link then us this

如果你不想它说错误的链接,那么我们这个

<center>
<script>
function goBack()
  {
  window.history.back()
  }
</script>
<input type="button" value="Go Back" onclick="goBack()"/>

And you can edit what the button says by changing the "Go Back" next to value= in the last line.

您可以通过更改最后一行中 value= 旁边的“返回”来编辑按钮的内容。