gpt4 book ai didi

javascript - 错误 : Cannot read property 'top' of null

转载 作者:太空宇宙 更新时间:2023-11-03 23:48:25 25 4
gpt4 key购买 nike

我想获得页脚的顶部。但是我收到此错误:无法读取 null 的属性“顶部”

这是我的html

<footer class="footer" role="complementary" id="myfooter">

</footer>

在 jquery 中

var topoffooter=$('#myfooter').offset().top;

谁能帮忙解决这个问题

最佳答案

问题

问题的发生可能是因为您的脚本无法找到 DOM 元素。该元素在被 javaScript 使用之前应该存在于 DOM 中


调试

而不是这一行 var topoffooter=$('#myfooter').offset().top;只是尝试提醒 jQuery 对象的长度

alert($('#myfooter').length);

它必须大于零才能工作,否则我们就走在解决问题的正确道路上。


解决方案

1) 您可以将脚本放在 body 标签结束之前 - </body> - 首选

2) 或者,使用 jQuery 的
在文档 (DOM) 就绪事件上执行脚本 $(document).ready() http://api.jquery.com/ready/

关于javascript - 错误 : Cannot read property 'top' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20947682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com