如何将字符串添加到window.location.hash
任何想法如何写这个:
var hashValue = "#" + window.location.hash.substr(1);
$([document.documentElement, document.body]).animate({
scrollTop: $(hashValue).offset().top
}, 500);
没有收到此错误:
未捕获错误:语法错误,无法识别的表达式:#
它的工作原理是这样的,但我不想在控制台中看到此错误。
Any ideas how can I write this:
var hashValue = "#" + window.location.hash.substr(1);
$([document.documentElement, document.body]).animate({
scrollTop: $(hashValue).offset().top
}, 500);
Without getting this error:
Uncaught Error: Syntax error, unrecognized expression: #
It is working like that, but I don't want to see this error in the console.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们假设 window.location.hash 是空字符串。在本例中,
hashValue
为"#"
。因此 jQuery 调用
$(hashValue)
无法将其识别为元素 ID,因此会引发语法错误。Let’s just assume
window.location.hash
is the empty string. In this casehashValue
is"#"
.So the jQuery call
$(hashValue)
doesn’t recognize it as an element ID, so it throws a syntax error.