我有一些片段加载点击。我也滚动页面到这些链接的顶部,如css-tricks上发现。我收到以下错误:未捕获的错误:语法错误,无法识别的表达式:#!Fragment_NamejQuery无法识别的表达与hashbang
我的JS
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);//this is where the error is
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
HTML
<li><a href="#!Fragment_Name">Link Text</a></li>
我已经试过VAR的目标= $($(本.hash));没有快乐
一切仍然有效,我只是想知道如何解决这个问题,并从控制台中删除错误。
我更换了var target = $(this.hash); var target = $(this.hash.replace(/([!])/ g,“\\ $ 1”));该错误现在读取未捕获的错误:语法错误,无法识别的表达式:[name =!Fragment_Name] –
@DirtyBirdDesign这是您的第二个选择器('$ ['[name ='+ this.hash.slice(1)+']'); ')。只需在'='和']'之前加引号:'$('[name =''+ this.hash.slice(1)+'“]');' –
非常棒!结合了两人的工作。我不明白/ [!]/g究竟做了什么,我知道它正在逃避“!”但不是为什么。我得到的第二部分,转义片中的“#”。非常感谢,很好的回答! –