Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未捕获类型错误:无法读取属性';顶部';按导航到其他页面的菜单项时的未定义值_Javascript_Html_Bootstrap 4 - Fatal编程技术网

Javascript 未捕获类型错误:无法读取属性';顶部';按导航到其他页面的菜单项时的未定义值

Javascript 未捕获类型错误:无法读取属性';顶部';按导航到其他页面的菜单项时的未定义值,javascript,html,bootstrap-4,Javascript,Html,Bootstrap 4,当我在手机上使用菜单时,我的菜单有些问题。 在桌面上,菜单功能很好,但当我在手机上使用它时,我无法导航到另一页,它只在滚动到同一页上的id时才起作用。 当我按下菜单项:Idalium dawn时,我收到错误消息:Uncaught TypeError:无法读取未定义的属性“top”。我希望有人能帮助我:) 菜单的HTML代码: <nav class="site-navigation position-relative text-right" role="navi

当我在手机上使用菜单时,我的菜单有些问题。 在桌面上,菜单功能很好,但当我在手机上使用它时,我无法导航到另一页,它只在滚动到同一页上的id时才起作用。 当我按下菜单项:Idalium dawn时,我收到错误消息:Uncaught TypeError:无法读取未定义的属性“top”。我希望有人能帮助我:)

菜单的HTML代码:

<nav class="site-navigation position-relative text-right" role="navigation">
  <ul class="site-menu main-menu js-clone-nav mr-auto d-none d-lg-block">
    <li><a href="#home-section" class="nav-link">Home</a></li>
    <li class="nav-item dropdown">
      <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        Products
      </a>
      <div class="dropdown-menu" aria-labelledby="navbarDropdown">
        <a class="dropdown-item dropdown-text dropdown-header" href="#products-section">All products</a>
        <a class="dropdown-item dropdown-text" href="idalium-dawn.html">Idalium Dawn</a>
      </div>
    </li>
    <li><a href="#agents-section" class="nav-link">Agents</a></li>
    <li><a href="#about-section" class="nav-link">About</a></li>
    <li><a href="#news-section" class="nav-link">News</a></li>
    <li><a href="#contact-section" class="nav-link">Contact</a></li>
  </ul>
</nav>

Idalium dawn链接没有散列,因为它是外部链接,所以$(hash).offset()未定义,这就是错误的原因

为了防止此错误,您可以将滑块代码包装为这样的条件

var hash = this.hash;
if(hash) {
  e.preventDefault();
  $('html, body').animate({
    'scrollTop': $(hash).offset().top
  }, 600, 'easeInOutExpo');
}   

谢谢你的回复,我很难调整它。您建议更改什么?错误消息现在已消失,但它没有更改站点的行为。它仍然无法将我重定向到另一个页面。您确定要隐藏e.preventDefault();内部条件?对不起,我确实忘记了,现在它在内部条件下工作。谢谢!我刚刚注意到,当url在文件后面包含一个#时,它仍然会给出错误,例如:index.html#test,是否也可以解决这个问题?
var hash = this.hash;
if(hash) {
  e.preventDefault();
  $('html, body').animate({
    'scrollTop': $(hash).offset().top
  }, 600, 'easeInOutExpo');
}