【问题标题】:go on first page not working继续第一页不起作用
【发布时间】:2017-04-01 07:27:48
【问题描述】:

我正在尝试创建一个按钮以进入第一个历史记录页面,但我不知道为什么我的代码不起作用。这是我的代码:

function firstpg() {
  var startpt = '-' + window.history.length;
  window.history.go(startpt);
}
<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <button onclick="firstpg()">Go First</button>
  </body>
</html>

有什么问题?

【问题讨论】:

  • 您是否需要添加-1,因为length 总是包含一些额外的内容?

标签: javascript html browser-history


【解决方案1】:

history 还包括当前页面,因此您需要从长度中减少1 才能转到第一页。

var startpt = '-' + ( window.history.length - 1) ;

var startpt = -(window.history.length - 1) ;

【讨论】:

    【解决方案2】:

    您需要否定length,因为它不是基于0。所以试试:

    var startpt = ( window.history.length - 1) * -1;
    

    这应该可行:

    <!DOCTYPE html>
    <html>
      <head>
        <script>
          function firstpg() {
            var startpt = ( window.history.length - 1) * -1;
            window.history.go(startpt);
          }
        </script>
      </head>
      <body>
        <button onclick="firstpg()">Go First</button>
      </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2016-05-02
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 2018-11-30
      相关资源
      最近更新 更多