插件在 jQuery 2.2.0 更新后停止工作
Plugin stopped working after jQuery 2.2.0 update
我有小的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");
}
});
我尝试调试,但它抛出
Error: Syntax error, unrecognized expression: [href=#baz]
这是jsFiddle: http://jsfiddle.net/up4nu/5473/
最新的jQuery 2.2.0中的哪些更新一定导致了此错误?
您需要转义#
符号。演示。
end().filter("[href='"#"+id+"'"]").parent().addClass("active");
或者,您可以使用''#
.演示。
end().filter("[href=''#"+id+"]").parent().addClass("active");
相关文章:
- jQuery UI自动完成突然停止工作
- JavaScript打印功能使日历停止工作
- 下拉列表在使用z索引放置在前面后停止工作
- 由于响应中不存在“Access Control Allow Origin”标头,跨域请求停止工作
- 注入工厂时,Angular停止工作
- React Native DeviceEventEmitter键盘WillShow停止工作
- JavaScript在更改css时停止工作
- Chrome地理定位在更新后停止工作
- 重新启动jquery脚本后,角度停止工作
- 更新Wordpress和我的平滑滚动停止工作
- 为什么在ASP.NET Ajax更新面板中执行任何操作后javascript元素都停止工作
- visualstudio2013更新4-javascript highlithning停止工作
- 在jQuery.html()更新时,其他函数将停止工作
- Browser.ExecScript() 在更新窗口后停止工作
- 插件在 jQuery 2.2.0 更新后停止工作
- Javascript代码的某些部分在最新的Chrome更新中停止工作
- 在最近的更新与代码传输停止工作后:tsc未被识别为内部或外部命令
- Cordova应用程序在节点和/或Cordova更新后停止工作
- jQuery tablesorter:过滤器和斑马小部件停止工作后,ajax更新IE8仅
- AppEngine API客户端认证在更新后停止工作