如何修复 document.body 为空错误

发布于 2024-08-08 01:25:36 字数 857 浏览 4 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

美人如玉 2024-08-15 01:25:36

我同意 outis - 我们需要更多代码。

也就是说:

  • 您确定您的所有工作都在 $(document).ready 函数内完成吗?
  • 您确实有正确形成(且闭合)的 body 标记和有效的 xhtml 吗?
  • 所有浏览器都会出现这种情况吗?

一个建议:

如果您将 positioneerElement 函数也放入 $(document).ready 函数中,问题是否仍然存在?我想知道浏览器是否在文档加载之前评估 document.body 。

$(document).ready(function() {
    function positioneerElement(){      
    $breedte = (document.body.clientWidth - 1124) / 2;
    $('#bg_left').css({
        'width': $breedte
    });
    $('.container').css({
        'margin-left': $breedte
    });

    // your other code, including
    positioneerElement();
});

I agree with outis - we need some more code.

That said:

  • Are you sure you're doing all your work inside the $(document).ready function?
  • Do you definitely have a correctly formed (and closed) body tag and valid xhtml?
  • Does this happen on all browsers?

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.

$(document).ready(function() {
    function positioneerElement(){      
    $breedte = (document.body.clientWidth - 1124) / 2;
    $('#bg_left').css({
        'width': $breedte
    });
    $('.container').css({
        'margin-left': $breedte
    });

    // your other code, including
    positioneerElement();
});
秋凉 2024-08-15 01:25:36

确保仅在包含 标记后调用 $(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 of document.documentElement.clientWidth. If you haven't started your <body> yet, there is not document.body node present.

能否归途做我良人 2024-08-15 01:25:36

您必须包含 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

九歌凝 2024-08-15 01:25:36

好吧,根据您给我的信息,可能是主体尚未正确加载。请参阅此处了解更多详细信息: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文