我有一个固定导航,点击导航项并平滑滚动到页面上的部分,并向菜单项添加活动类。
当在localhost上进行本地开发时,它工作得很好但是现在我已经将网站移动了,它给出了错误:
Uncaught Error: Syntax error, unrecognized expression: /#about-section
和
throw new Error("Syntax error, unrecognized expression: " + a)
该网站位于website to test 使其在localhost上运行的代码是:
jQuery(document).ready(function($) {
var sections = $('section'),
nav = $('nav'),
nav_height = nav.outerHeight();
$(window).on('scroll', function() {
var cur_pos = $(this).scrollTop();
sections.each(function() {
var top = $(this).offset().top - nav_height,
bottom = top + $(this).outerHeight();
if (cur_pos >= top && cur_pos <= bottom) {
nav.find('a').removeClass('active');
sections.removeClass('active');
$(this).addClass('active');
nav.find('a[href="#' + $(this).attr('id') + '"]').addClass('active');
}
});
});
nav.find('a').on('click', function() {
var $el = $(this),
id = $el.attr('href');
$('html, body').animate({
scrollTop: $(id).offset().top - nav_height
}, 500);
return false;
});
});
&#13;
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul id="menu-not-landing-main-menu" class="nav navbar-nav navbar-right">
<li id="menu-item-442" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-442 active active">
<a href="/#about-section">About</a>
</li>
<li id="menu-item-443" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-443 active active">
<a href="/#speakers-section">Speakers</a>
</li>
<li id="menu-item-444" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-444 active active">
<a href="/#mini-zeminars-section">Mini Zeminars</a>
</li>
</ul>
</div>
&#13;
有人可以帮忙吗?
答案 0 :(得分:1)
从所有锚标记/
中删除斜杠href
,例如
<li id="menu-item-442" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-442 active active">
<a href="#about-section">About</a>
</li>
<li id="menu-item-443" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-443 active active">
<a href="#speakers-section">Speakers</a>
</li>
<li id="menu-item-444" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-444 active active">
<a href="#mini-zeminars-section">Mini Zeminars</a>
</li>
你会很高兴;)
答案 1 :(得分:0)
id 无法以 / 开头,因此请将其分割出来:
var **target** = *id.slice(1)*;
然后,
scrollTop: $(**target**).offset().top - nav_height