在测试我正在构建的网站时,我在页面上滚动时出现此错误:未捕获错误:语法错误,无法识别的表达式
我有一个带有高光照导航的单页网站。我知道错误是由ID和引用语法引起的。我也知道自jquery 1.5以来,引用属性值是必需的,你可以引用单引号或双引号。但考虑到这一点,我无法通过尝试单引号和双引号的不同变体来消除错误。为什么在滚动页面时会出现此错误?
来自Jquery的错误是一个嘶嘶声的错误。
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
var divPos = $(theID).offset().top - 200; // get the offset of the div from the top of page
var divHeight = $(theID).height(); // get the height of the div in question
if (windowPos >= divPos && windowPos < (divPos + divHeight)) {
$("a[href='"+ theID +"']").addClass("nav-active");
} else {
$("a[href='"+ theID +"']").removeClass("nav-active");
}
}
// $('html, body').animate({scrollTop: $('faq').offset().top -600 });
if(windowPos + windowHeight == docHeight) {
if (!$("div li:last-child a").hasClass("nav-active")) {
var navActiveCurrent = $("nav-active").attr("href");
$("a[href='"+ navActiveCurrent+ "']").removeClass("nav-active");
$("div li:last-child a").addClass("nav-active");
}
}
});
答案 0 :(得分:0)
我能够通过取出var divHeight = $(theID).height();
并添加一个关于divPosid长度的if语句来解决此错误。
此外,我还有常规标题,确保只有我的粘贴标题有<li></li>
个标记。该错误是在列表项标记中包含常规标题的结果。这个答案也帮助我解决了这个问题:jQuery Syntax error, unrecognized expression
$(window).scroll(function(){
var windowPos = $(window).scrollTop(); // get the offset of the window from the top of page
var windowHeight = $(window).height(); // get the height of the window
var docHeight = $(document).height();
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
var divPosid = $(theID);
if (!divPosid.length) {
return;
}
var divPos = divPosid.offset().top - 200; // get the offset of the div from the top of page
var divHeight = $(theID).height(); // get the height of the div in question
if (windowPos >= divPos && windowPos < (divPos + divHeight)) {
$("a[href='"+ theID +"']").addClass("nav-active");
} else {
$("a[href='"+ theID +"']").removeClass("nav-active");
}
}
if(windowPos + windowHeight == docHeight) {
if (!$("div li:last-child a").hasClass("nav-active")) {
var navActiveCurrent = $(".nav-active").attr("href");
$("a[href='"+ navActiveCurrent+ "']").removeClass("nav-active");
$("div li:last-child a").addClass("nav-active");
}
}
});