滚动到顶部按钮在子模板中不起作用

Scroll To Top Button not working in Child templates

我已经在我的 Base 模板中添加了 Scroll Back To Top Button,它运行良好。但是当我转到任何其他页面时,该按钮将不起作用。为什么?!

这是我的基本模板:

<!DOCTYPE html>
<html>
  <head></head>

  <body>
    <button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
    .
    .
    <div class="container">
      #import("content") 
    </div>
    .
    .
  </body>
</html>

我正在使用上面的 JavaScript link:

window.onscroll = function() { scrollFunction() };

function scrollFunction() {
  if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) {
    $('#myBtn').fadeIn();
  } else {
    $('#myBtn').fadeOut();
  }
}

function topFunction() {
  document.body.scrollTop = 0; // For Safari
  document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}

该按钮仅在主页上有效,但在任何其他页面上无效,即使我看到(来自查看页面源代码)<button onclick="topFunction()" id="myBtn" title="Go to top">Top</button> 在正文中。有人可以帮忙吗??

上面的代码实际上是正确的,按钮现在在每个页面上都能正常工作。在 tobygriffin 指出后,我注意到有一个 Cannot read property 'style' of undefined 错误,我以前没有。修复该错误后,该按钮可在所有页面上使用。

尽管我犯了一个愚蠢的错误,但我保留了这个问答(而不是删除它)希望有人会发现它有用:)