简体   繁体   English

输入类型 - 后退按钮?

[英]input type - back button?

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

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">

"Best" is a little subjective. “最好”有点主观。

@Mech Software has an acceptable solution, as it's a simple function call. @Mech Software有一个可接受的解决方案,因为它是一个简单的函数调用。 If you're only making one back button, it's probably the way to go. 如果你只做一个后退按钮,那么可能就是这样。 However, I try to place code where it belongs: 但是,我尝试将代码放在它所属的位置:

HTML belongs in .html, CSS belongs in .css, and JavaScript belongs in .js files. HTML属于.html,CSS属于.css,JavaScript属于.js文件。 Instead of giving the button an onclick attribute, I'd give it a class of back-button and attach the click listener to each .back-button element. 我给它一个back-button并将click监听器附加到每个.back-button元素,而不是给按钮一个onclick属性。

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()"每个实例onclick="history.back()"

<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()"每个实例onclick="history.back()"

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 =旁边的“Go Back”来编辑按钮所说的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM