无法读取属性'top'未定义的(重复2次)

Cannot read property 'top' of undefined (repeated 2 times)

本文关键字:重复 2次 未定义 读取 top 属性      更新时间:2023-09-26

使用自定义WordPress模板,一切都很好。但是chrome调试器在控制台中显示Uncaught TypeError:

无法读取未定义的属性"top"(重复2次)它链接到funiction.js:中的代码

function goto(id, t){
//animate to the div id.
jQuery(".container-wrapper").animate({"top": -(jQuery(id).position().top)}, 1500, 'easeInOutCubic');
}

如何修复它会很有帮助!非常感谢。

我认为在chrome的调试器上会出现此错误,因为在执行jQuery(id)时,jQuery找不到具有该ID的东西,因此返回一个空数组([])。对该空数组调用.position()无效,并返回undefined。尝试访问未定义对象的顶端值是非法的,并打印此错误。

这就是为什么你会出现这个错误。尝试编辑代码以测试jQuery(id).position()是否为undefined

function goto(id, t) {
    if($(id).position() !== undefined) {
        //animate to the div id. 
        jQuery(".container-wrapper").animate({"top": -(jQuery(id).position().top)}, 1500, 'easeInOutCubic');
    }
}

提示:使用$而不是jQuery访问jQuery功能