当我在此函数中使用 scrollTo jQuery 插件时,我收到一条“无法读取未定义的属性‘切片’”消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8090075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I get an `Cannot read property 'slice' of undefined` message when I use the scrollTo jQuery plugin inside this function
提问by alexchenco
I'm using the jQuery scrollToplugin.
我正在使用 jQuery scrollTo插件。
I get this error in my JS Console:
我在 JS 控制台中收到此错误:
16827Uncaught TypeError: Cannot read property 'slice' of undefined
d.fn.scrollToindex.html.js:16827
jQuery.extend.eachindex.html.js:662
d.fn.scrollToindex.html.js:16827
jQuery.extend.eachindex.html.js:662
jQuery.fn.jQuery.eachindex.html.js:276
d.fn.scrollToindex.html.js:16827
popupPlaceindex.html.js:18034
(anonymous function)index.html.js:17745
jQuery.extend._Deferred.deferred.resolveWithindex.html.js:1018
doneindex.html.js:7247
jQuery.ajaxTransport.send.script.onload.script.onreadystatechange
When I place $(".menu").scrollTo( $("li.matched").attr("id"), 800 );
inside it.
当我把$(".menu").scrollTo( $("li.matched").attr("id"), 800 );
它放进去的时候。
function popupPlace(dict) {
$popup = $('div#dish-popup');
$popup.render(dict,window.dishPopupTemplate);
if(typeof(dict.dish) === 'undefined') {
$popup.addClass('place-only');
} else {
$popup.removeClass('place-only');
}
var $place = $('div#dish-popup div.place');
var place_id = dict.place._id;
if(liked[place_id]) {
$place.addClass('liked');
} else {
$place.removeClass('liked');
}
if(dict.place.likes) {
$place.addClass('has-likes');
} else {
$place.addClass('zero-likes');
}
var tokens = window.currentSearchTermTokens;
var tokenRegex = tokens && new RegExp($.map(tokens, RegExp.escape).join('|'), 'gi');
$.each(dict.place.products, function(n, product) {
$product = $('#menu-item-'+product.id);
if(liked[place_id+'/'+product.id]) {
$product.addClass('liked');
}
if(tokens && matchesDish(product, tokens)) {
$product.addClass('matched');
$product.highlight(tokenRegex);
} else {
$product.removeClass('matched');
$product.removeHighlight();
}
if(product.likes) {
$product.addClass('has-likes');
} else {
$product.addClass('zero-likes');
}
});
$('#overlay').show();
$('#dish-popup-container').show();
// Scroll to matched dish
//$("a#scrolll").attr("href", "#" + $("li.matched").attr("id"));
//$("a#scrolll").attr("href", "#" + $("li.matched").attr("id"));
//$("a#scrolll").trigger("click");
$(".menu").scrollTo( $("li.matched").attr("id"), 800 );
// Hide dish results on mobile devices to prevent having a blank space at the bottom of the site
if (Modernizr.mq('only screen and (max-width: 640px)')) {
$('ol.results').hide();
}
$(".close-dish-popup").click(function() {
$("#overlay").hide();
$("#dish-popup-container").hide();
$('ol.results').show();
changeState({}, ['dish', 'place', 'serp']);
});
showPopupMap(dict.place, "dish-popup-map");
}
Any suggestion to fix this?
有什么建议可以解决这个问题吗?
回答by darkhouse
What are you trying to scroll to? I found my problem was that I was trying to scroll to an anchor, but I had only specified the name, not the id. If you just set the id the same as the name, it should work, assuming that is your issue.
你想滚动到什么?我发现我的问题是我试图滚动到一个锚点,但我只指定了名称,而不是 id。如果您只是将 id 设置为与名称相同,那么它应该可以工作,假设这是您的问题。
回答by boena
I just opened a pull request that will fix the problem with targets that don't exist.
我刚刚打开了一个拉取请求,它将解决不存在的目标的问题。
You can see the change on my fork: https://github.com/boena/jquery.scrollTo
你可以在我的 fork 上看到变化:https: //github.com/boena/jquery.scrollTo