未捕获错误:语法错误,无法识别表达式Jquery选择器单引号与双引号
Uncaught Error: Syntax error, unrecognized expression Jquery selector single vs double quotes
当测试我正在构建的网站时,当我在页面上滚动时,我会保留此错误:未捕获错误:语法错误,无法识别的表达式
我有一个高光导航的单页网站。我知道这个错误是由ID和引号语法引起的。我还知道,由于jquery1.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");
}
}
});
我可以通过去掉var divHeight = $(theID).height();
并添加一个关于divPosid长度的if语句来解决这个错误。
此外,我有我的常规标题,确保只有我的粘性标题有<li></li>
标签。该错误是由于我的常规标头位于列表项标记中所致。这个答案也帮助我解决了这个问题:jQuery语法错误,无法识别的表达式
$(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");
}
}
});
相关文章:
- Javascript/jQuery:用HTML span标记替换单引号中的文本
- 删除单引号字符转义
- 将字符串中的单引号替换为双引号
- JavaScript:单引号'导致错误
- 如何通过HTML将带有单引号的JSON从PHP传递到Javascript
- 如何在java脚本中用单引号写逗号分隔的值
- 使用jquery或regex,带点但不带点的拆分字符串位于单引号中
- 用空格替换单引号和其他符号
- javascript单引号/双引号
- 如何在字符串输入的 Html 元素中的双引号内编写单引号
- 使用javascript为#、单引号和双引号编码值
- JavaScript 相等操作对单引号字符串和数字都成功
- 对字符串使用单引号或双引号
- 如何在javasciipt中添加单引号到字符串,该字符串将在nodeJS中作为mysql插入命令执行
- JavaScript:当存在单引号时,用于替换双花引号的正则表达式失败
- 单引号 JSP 到 JavaScript 函数
- 在文本区域中,输入用jquery str.replacement()替换哭泣的smiley live的单引号
- json单引号错误
- 未捕获错误:语法错误,无法识别表达式Jquery选择器单引号与双引号
- 如何修复这个java脚本错误处理单引号在行