错误:无法读取属性“顶部”为null [英] error: Cannot read property 'top' of null
问题描述
我想得到我的footer的顶部,但我得到这个错误:无法读取属性'顶部'null
这里是我的html
< footer class =footerrole =complementaryid =myfooter>
< / footer>
和jquery
var topoffooter = $('#myfooter')。offset()。
可以帮助任何人。
问题
问题可能发生,因为您的脚本无法找到DOM元素。
元素应该在java中使用之前在DOM中。
DEBUGGING
而不是这行 var topoffooter = $('#myfooter')。offset()。top;
只尝试提醒jQuery对象的长度
alert($('#myfooter')。
它必须大于零才能工作,如果不是,我们就在正确的路径上解决
> 1)您可以将脚本放在body标记 -
< / body>
- preferred b 2)或者,使用jQuery的
$(document).ready()
http://api.jquery.com/ready/
i want to get the top of my footer.but i am getting this error: Cannot read property 'top' of null
here is my html
<footer class="footer" role="complementary" id="myfooter">
</footer>
and in jquery
var topoffooter=$('#myfooter').offset().top;
can anybody help regarding this
PROBLEM
The problem might be happening because your script is not able to find the DOM element. The element should be there in the DOM before it is used by javaScript
DEBUGGING
Instead of this line var topoffooter=$('#myfooter').offset().top;
just try to alert the length of jQuery object
alert($('#myfooter').length);
It must be greater than zero to work, and if not we are on the right path to solve the issue.
SOLUTION
1) You can put the scripts just before the closing of body tag - </body>
- PREFERRED
2) Or, execute your scripts on document(DOM) ready event using jQuery's
$(document).ready()
http://api.jquery.com/ready/
这篇关于错误:无法读取属性“顶部”为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!