Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery 2.2.0更新后插件停止工作_Javascript_Jquery - Fatal编程技术网

Javascript jQuery 2.2.0更新后插件停止工作

Javascript jQuery 2.2.0更新后插件停止工作,javascript,jquery,Javascript,Jquery,我有一个小scrollspy片段来观察DOM中的滚动事件 代码段在jQuery 2.1.4之前工作正常,但在最新的jQuery 2.2.0中工作不正常 代码如下: // Cache selectors var lastId, topMenu = $("#top-menu"), topMenuHeight = topMenu.outerHeight()+15, // All list items menuItems = topMenu.find("a"),

我有一个小scrollspy片段来观察DOM中的滚动事件

代码段在jQuery 2.1.4之前工作正常,但在最新的jQuery 2.2.0中工作不正常

代码如下:

// Cache selectors
var lastId,
    topMenu = $("#top-menu"),
    topMenuHeight = topMenu.outerHeight()+15,
    // All list items
    menuItems = topMenu.find("a"),
    // Anchors corresponding to menu items
    scrollItems = menuItems.map(function(){
      var item = $($(this).attr("href"));
      if (item.length) { return item; }
    });

// Bind click handler to menu items
// so we can get a fancy scroll animation
menuItems.click(function(e){
  var href = $(this).attr("href"),
      offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+1;
  $('html, body').stop().animate({ 
      scrollTop: offsetTop
  }, 300);
  e.preventDefault();
});

// Bind to scroll
$(window).scroll(function(){
   // Get container scroll position
   var fromTop = $(this).scrollTop()+topMenuHeight;

   // Get id of current scroll item
   var cur = scrollItems.map(function(){
     if ($(this).offset().top < fromTop)
       return this;
   });
   // Get the id of the current element
   cur = cur[cur.length-1];
   var id = cur && cur.length ? cur[0].id : "";

   if (lastId !== id) {
       lastId = id;
       // Set/remove active class
       menuItems
         .parent().removeClass("active")
         .end().filter("[href=#"+id+"]").parent().addClass("active");
   }                   
});
以下是JSFIDLE:

最新jQuery 2.2.0中的哪些更新一定导致了此错误?

您需要转义
#
符号

或者,您可以使用
\\\\\


这有用吗
[href=“#baz”]
你不认为这在
jQuery
的任何版本中都会失败吗?@RayonDabre原始代码实际上可以与jQuery 2.1.4配合使用。@Yuki,刚刚测试了这一点,我很惊讶……你需要提到它是特定于版本的,尽管它看起来不是这样的。。
Error: Syntax error, unrecognized expression: [href=#baz]
end().filter("[href=\"#"+id+"\"]").parent().addClass("active");
end().filter("[href=\\#"+id+"]").parent().addClass("active");