1

我有一个代码:

(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 中,我将截断页面。然后我在控制台中得到一个错误。

> Uncaught TypeError: Cannot read property 'nodeName' of null
>>    at _e (index.js:63)
>>>  at MutationObserver.<anonymous> (index.js:63)

Wordpress 中的页面。任何人都可以帮忙吗?

4

3 回答 3

1

无法读取 null 的属性“nodeName”表明您尝试从 jQuery 访问的内容在脚本尝试访问它时不可用。

如果没有看到其余的代码,就很难判断缺少什么,但作为起点,请确保您的 jQuery 函数被 document on ready 函数包围。

// 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 完全加载后运行。

于 2021-01-21T13:29:30.433 回答
0

您收到此错误是因为您的 javascript 库在加载 DOM 之前加载。确保首先加载您的 DOM。

于 2021-07-15T16:23:36.210 回答
0

该错误表明您正在尝试读取nodeName从您认为是对象的空值变量调用的属性。请检查代码的其他部分。

于 2021-01-21T14:43:44.367 回答