如何修复 document.body 为空错误
我的 javascript 中出现 document.body is null 错误,因为我使用:
$(window).width()
作为值来分配给我的变量,
$(document).ready(function(){});
我将非常感谢任何可以帮助我解决此问题的人。
亲切的问候
编辑:抱歉,如果这一切都不清楚。我有一个演示:http://www.wpmonk.com/demo/hypowired 起初主题会加载,但随后变成白色(由于错误),但是当您重新加载时,您可以看到整个主题,因为然后他知道 $(window).width() 的值,
我使用此代码将主题居中布局(对于 css 来说是不可能的,因为左侧也需要有宽度。)
function positioneerElement(){
$breedte = (document.body.clientWidth - 1124) / 2;
$('#bg_left').css({
'width': $breedte
});
$('.container').css({
'margin-left': $breedte
});
}
我在加载函数中调用函数 positioneerElement()
。
抱歉,如果不清楚,我认为没有必要将演示放在这里,也没有必要将代码放在这里。 不过我还是要感谢那些试图提供帮助的人!
I'm having a document.body is null error in my javascript because I use:
$(window).width()
as value to assign to a variable in my
$(document).ready(function(){});
I would be very grateful to anyone who could help me with this.
Kind regards
edit: sorry if this all was unclear. I have a demo at: http://www.wpmonk.com/demo/hypowired
at first the theme will load but then becomes white (because of the error) but when you reload you can see the whole theme because then he knows the value for $(window).width()
I'm using this code to center the layout (not possible with css because the left needs to have a width aswell.)
function positioneerElement(){
$breedte = (document.body.clientWidth - 1124) / 2;
$('#bg_left').css({
'width': $breedte
});
$('.container').css({
'margin-left': $breedte
});
}
I call function positioneerElement()
in the load function.
Sorry if this wasn't clear I though it wasn't necessary to put the demo here for this nor the code.
However I would like to thank the people who are trying to help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我同意 outis - 我们需要更多代码。
也就是说:
一个建议:
如果您将
positioneerElement
函数也放入 $(document).ready 函数中,问题是否仍然存在?我想知道浏览器是否在文档加载之前评估 document.body 。I agree with outis - we need some more code.
That said:
One suggestion:
Does the problem persist if you put the
positioneerElement
function in the $(document).ready function as well? I'm wondering whether the browser evaluates document.body prior to the document loading.确保仅在包含
标记后调用
$(window).width()
回调),和/或使用标准模式文档类型声明。无论如何你都应该这样做:怪癖模式很糟糕。问题是,要在 Quirks 模式下读取视口大小,jQuery 必须查看
document.body.clientWidth
而不是document.documentElement.clientWidth
。如果您尚未启动,则不存在
document.body
节点。Make sure you only call
$(window).width()
after you have included the<body>
tag (eg. in the ready callback), and/or use a Standards Mode doctype declaration. You should be doing that anyway: Quirks Mode sucks.The issue is, to read the viewport size in Quirks Mode jQuery must look at
document.body.clientWidth
instead ofdocument.documentElement.clientWidth
. If you haven't started your<body>
yet, there is notdocument.body
node present.您必须包含 jquery.js,而不是 jquery.min.js
看起来好像有一些错误
这对我有用
You have to include jquery.js, and not jquery.min.js
It looks like some bug in it
This works for me
好吧,根据您给我的信息,可能是主体尚未正确加载。请参阅此处了解更多详细信息: http ://www.coderanch.com/t/122834/HTML-JavaScript/document-body-null-or-not#606782
我会说更多,但我也同意你需要发布更多代码供我们调试。
Well, just based on the information you have given me it might be that the body has not loaded properly yet. Please see here for more detials: http://www.coderanch.com/t/122834/HTML-JavaScript/document-body-null-or-not#606782
I would say more but I too agree with outis that you need to post some more code for us to debug.