javascript - 未捕获的类型错误 : Cannot read property 'nodeName' of null

标签 javascript nodename

我有一个代码:

(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/

相关文章:

javascript - .tagName 和 .nodeName 的区别

HADOOP - 在映射器中获取节点名

javascript - Zurb 基金会 Reveal Modal 从窗口底部打开

javascript - 是否可以使用上下文和引用全局存储 react 元素的尺寸?

javascript - 如何使用滚动函数获取的位置获取 HTML 中的元素标签

javascript - a.nodeName is undefined Jquery错误

ssl - puppet 大师没有将代理主机名/fqdn 传递给 enc 脚本

javascript - 如何在 View 中将变量从 Controller 传递到 JavaScript?

javascript - 在用户导航到其他 URL 之前触发的浏览器事件

java - 将 #text 从 getNodeName 转换为字符串