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

标签 javascript jquery

我有以下代码,如果您登陆带有主题标签的页面,并且后面有文本,就像这样 #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

它输出此错误未捕获错误:语法错误,无法识别的表达式:

? 标记之后不读取任何内容

我做错了什么?

最佳答案

话题标签是一个自动超链接的搜索词。您没有主题标签,您有 fragment identifier ,但您的实现不正确。

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

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

关于javascript - 窗口位置主题标签语法错误未在问号后读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35002578/

相关文章:

javascript - MVCarray 和数组的区别

javascript - 如何在调整窗口大小时保持 chartjs/ng2-charts 渐变?

javascript - preventDefault() 第一次触摸但仍然允许滚动/触摸移动

php - 每页有多个表单的 jQuery forms.js

javascript - 如何让 jQuery 在每个 iframe 中默认可用?

javascript - 迭代有效 JSON 时无法将 undefined 或 null 转换为对象

javascript - 在数组 Node.js 中组合 JSON 数组

javascript - d3js 给出类型错误 "d is undefined"

javascript - 如何修复浏览器移动 View 中未定义问题的 jQuery 'length'?

javascript - 使用 javascript 创建嵌套的 div