单击菜单栏时浏览器地址栏显示#[object%20Object]

Browser address bar displays #[object%20Object] when clicking the menubar

我发现如果单击菜单栏中的某个部分,浏览器地址栏会显示 #[object%20Object]。但是没有影响scroll-to-section.

参考部分代码:

<li class="scroll-to-section"><a href="#top" class="active">Home</a></li>
<li class="scroll-to-section"><a href="#services">Services</a></li>
<li class="scroll-to-section"><a href="#about">About</a></li>
<li class="scroll-to-section"><a href="#users">Evaluations</a></li>
<li class="scroll-to-section"><a href="#newsletter">Newsletter</a></li>

JavaScript代码:

$('.scroll-to-section a[href*=\#]:not([href=\#])').on('click', function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        var width = $(window).width();
        if(width < 991) {
          $('.menu-trigger').removeClass('active');
          $('.header-area .nav').slideUp(200);
        }
        $('html,body').animate({
          scrollTop: (target.offset().top) + 1
        }, 700);
        return false;
      }
    }
  });
$(document).ready(function () {
      $(document).on("scroll", onScroll);
      
      //smoothscroll
      $('.scroll-to-section a[href^="#"]').on('click', function (e) {
          e.preventDefault();
          $(document).off("scroll");
          
          $('.scroll-to-section a').each(function () {
              $(this).removeClass('active');
          })
          $(this).addClass('active');
          var target = this.hash;
          menu = target;
          target = $(this.hash);
          $('html, body').stop().animate({
              scrollTop: (target.offset().top) + 1
          }, 500, 'swing', function () {
              window.location.hash = target;
              $(document).on("scroll", onScroll);
          });
      });
  });

我尝试更新 jQuery,但没有成功。

Problematic Page

此问题是由var target = this.hash引起的,您可以删除此行。

target 被包含目标元素的 jQuery 对象覆盖。

window.location.hash = target;

这就是问题所在。您不能将 location.hash 设置为元素。它需要是文档中的 a nameid。一个字符串。不是对象。