未捕获错误:语法错误,无法识别表达式Jquery选择器单引号与双引号

Uncaught Error: Syntax error, unrecognized expression Jquery selector single vs double quotes

本文关键字:错误 单引号 选择器 Jquery 表达式 语法 识别      更新时间:2023-09-26

当测试我正在构建的网站时,当我在页面上滚动时,我会保留此错误:未捕获错误:语法错误,无法识别的表达式

我有一个高光导航的单页网站。我知道这个错误是由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");
          }
      }
  });