Jquery scrollTo(0,0)-对象不是函数
为什么我会犯这个错误 在控制台中,我可以在错误处放置调试器并键入:Jquery scrollTo(0,0)-对象不是函数,jquery,Jquery,为什么我会犯这个错误 在控制台中,我可以在错误处放置调试器并键入: window Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…} window.scrollTo(0,0) TypeError: object is not a function 为什么我会在这里出错?前几天的工作还不错 代码如下: (function ($) { $(document).re
window
Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}
window.scrollTo(0,0)
TypeError: object is not a function
为什么我会在这里出错?前几天的工作还不错
代码如下:
(function ($) {
$(document).ready(function () {
$('#customer-info-submit').click(function(e){
e.preventDefault();
var form = $('form#customer_information');
var i = 0;
$.each(required_fields, function(field, response) {
i++;
if( '' == $('#'+field).val() ) {
debugger;
$(gateway_message).html(response);
window.scrollTo(0,0);
return false;
}
console.log(i);
if( i == 9 ) {
debugger;
}
if( i == 12 ) { // 12 is the count of attributes of required fields
if( !$('#terms_agreement').is(':checked') ) {
$(gateway_message).html('You did not accept the Terms and Conditions.');
window.scrollTo(0,0);
} else {
form.submit();
}
}
});
return false;
});
});
}(jQuery));
我想您是想在jQuery版本中使用
scrollTop(0)
而不是scrollTo(0)
:
e、 g.$('body')。滚动顶部(0)代码>
当你打开窗口时。滚动到(0,0)
,我在Chrome F12调试器窗口中进行了尝试,效果很好
旁注:
使用快捷方式DOM ready事件处理程序组合IIFE(仅保证$scope)和$(document.ready()代码>
e、 g
jQuery将jQuery实例(自身)作为快捷方式DOM就绪处理程序中的第一个参数传递。和您的jQuery代码?您是否有一个名为scrollTo
的全局变量?(注意缺少var
关键字。)@RobertRozas您有什么建议?如果您只需在控制台中键入window.scrollTo
,您会得到什么?在Chrome中应该是函数scrollTo(){[native code]}
。@TrueBlueAussie我得到null!是的,这使阿明的回答无效,但提问者使用的是window.scrollTo()
(DOM方法),这确实应该是一个函数。@Frédéric Hamidi:是的。正如我所指出的,他的控制台代码在Chrome中运行良好。很难猜测是什么会干扰这一点/是的,我对此只有一个合理的解释,在评论中陈述了这一点,我仍在等待提问者跟进。@Frédéric Hamidi:当然可能。好建议。我将添加一些关于检查方法的评论。一些弹出窗口拦截器可能会干扰这一点。尝试关闭任何弹出窗口阻止程序,看看问题是否仍然存在。
jQuery(function($){
// your jQuery code using $
});