我有一个代码:
(function($) {
$(window).scroll(function() {
if ($(this).scrollTop() >= 50) { // If page is scrolled more than 50px
$('#return-to-top').fadeIn(200); // Fade in the arrow
} else {
$('#return-to-top').fadeOut(200); // Else fade out the arrow
}
});
$('#return-to-top').click(function() { // When arrow is clicked
$('body,html').animate({
scrollTop : 0 // Scroll to top of body
}, 500);
});
})(jQuery);
在 Chrome 中,我会向下 chop 页面。 然后我在控制台中收到错误。
> Uncaught TypeError: Cannot read property 'nodeName' of null
>> at _e (index.js:63)
>>> at MutationObserver.<anonymous> (index.js:63)
Wordpress 中的页面。有人可以帮忙吗?
最佳答案
Cannot read property 'nodeName' of null 表明您尝试从 jQuery 访问的内容在脚本尝试访问它时不可用。
如果没有看到代码的其余部分,就很难判断缺少什么,但作为一个起点,请确保您的 jQuery 函数被文档 on read 函数包围。
// A $( document ).ready() block.
$( document ).ready(function() {
$(window).scroll(function() {
if ($(this).scrollTop() >= 50) { // If page is scrolled more than 50px
$('#return-to-top').fadeIn(200); // Fade in the arrow
} else {
$('#return-to-top').fadeOut(200); // Else fade out the arrow
}
});
$('#return-to-top').click(function() { // When arrow is clicked
$('body,html').animate({
scrollTop : 0 // Scroll to top of body
}, 500);
});
});
这确保了代码块仅在 DOM 完全加载后运行。
关于javascript - 未捕获的类型错误 : Cannot read property 'nodeName' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65828295/