窗口位置主题标签语法错误在问号后未读取

Window location hashtag syntax error not reading after question mark

本文关键字:读取 错误 位置 标签 语法 窗口      更新时间:2023-09-26

我有以下代码,如果您像这样登陆带有主题标签和文本的页面,它可以正常工作#dashboard - 它应该降落在主题标签#whatever并滚动到该部分加载的位置。

$(document).ready(function() {
  // *only* if we have anchor on the url
  if (window.location.hash) {
    var hashname = $(window.location.hash);
    // smooth scroll to the anchor id
    $('html, body').animate({
      scrollTop: hashname.offset().top - 75
    }, 500);
  }
});

但是当你登陆这样的网址时

http://domain.com/#dasboard?utm_source=hubspot&utm_medium=ad&utm_campaign=stackoverflow

它将此错误输出Uncaught Error: Syntax error, unrecognized expression:

它在?标记后不读取任何内容

我做错了什么?

主题标签是一个自动超链接的搜索词。您没有主题标签,您有一个片段标识符,但您没有正确实现它。

片段标识符位于 URL 中的查询字符串之后,而不是之前。

浏览器正在解析#dasboard?utm_source=hubspot&utm_medium=ad&utm_campaign=stackoverflow作为您的选择器并遇到错误,因为您在其中有一个?