jQuery 从页面 B 滚动到页面 A 上的 ID (node, ejs)

jQuery scroll to ID on page A from page B (node, ejs)

这是我的导航栏

  <ul id="jump" class="dropdown-menu" role="menu">
      <li><a href="/main_page#top_something">People</a></li>
      <li><a href="/main_page">Main Page</a></li>
  </ul>

我正在使用这个功能

(function($){

var jump=function(e)
{
   if (e){
       e.preventDefault();
       var target = $(this).attr("href");
   }else{
       var target = location.hash;
   }

   $('html,body').animate(
   {
       scrollTop: $(target).offset().top
   },1000,function()
   {
       location.hash = target;
   });

}

$('html, body').hide()

$(document).ready(function()
{
    $('#jump a[href]').bind("click", jump);

    if (location.hash){
        setTimeout(function(){
            $('html, body').scrollTop(0).show()
            jump()
        }, 0);
    }else{
      $('html, body').show()
    }
});

})(jQuery)

main_page 是一个 ejs 文件,在 /main_page 路径上,node.js将呈现 main_page.ejs 文件。

我试图点击 A 页面上的导航栏并转到 div 部分的 B 页面。 这给我: jump.js:18 未捕获类型错误:无法读取未定义的 属性 'top' 错误

这是同一个主题,jQuery scroll to ID from different page但我无法正常工作...谢谢

区别在于我有 node.js 和 *.ejs 文件

(function (){
  if(location.hash === '#top_something'){
      window.setTimeout(
        function(){ 
            $('html, body').animate({
            scrollTop: $('#top_something').offset().top
            }, 'slow');
        },2000);
  }
})();

在这种情况下,这对我有用:)