本文介绍了为什么在Firefox中document.body == null,而不是Safari的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我正试图获得(一种为lightbox jQuery)的工作。由于FireFox(3.5.3)中的 document.body null ,所以不起作用。在Safari(4.0.3)中,colorbox并不是这种情况。



对我来说,有一点是(我在用Drupal 6)drupal有附加了一个脚本标签来设置页面底部的一些JavaScript变量,在关闭的body和html标签下面。否则我没有看到问题。不幸的是我有很多麻烦让它不这样做。难道这是导致FF与身体问题吗?



在Firefox中使用colorbox的示例文件确实有效( document.body 是在那里定义的)。

有什么方法可以使用jQuery来重新填充 document.body $()可能是$ c>属性,还是应该继续在drupal上敲击,以便不在html标签之外放置脚本标签(说起来容易做起来难) / p>

为了澄清 document.body null 页面完成加载。这里有一个Firebug控制台截图:

 >>> document.body 
null
>>> $()。attr('body')
null


解决方案

通常的原因 document.body 为null是因为您在< body> 标签(*)已经遇到。没有看到代码,我不能肯定地说,但这是通常的原因。移动放置脚本的位置。



(*:或其他元素,例如< p> 浏览器知道不能发生在一个主体之外,在这种情况下,它默默地添加一个< body> 来修正你的缺失标记。 -in-body-tags也许可以解释为什么Safari允许你避开它。)

没有。这是破碎的标记,Drupal不应该这样做,但关闭< / body> 不会停止 document.body 引用合适的DOM节点。

不, document.body 只是反映基本上与文档相同。 getElementsByTagName('body')[0] ,所以试图设置它是不明智的,如果你找不到 document.body ,jQuery将无法通过 $('body')找到它。


I have a problem with a page where I am trying to get colorbox (a kind of lightbox for jQuery) working. It doesn't work apparently due to the document.body being null in FireFox (3.5.3). This is not the case in Safari (4.0.3) where the colorbox works.

Something that jumps out at me is that (I'm using Drupal 6) drupal has appended a script tag to set some JavaScript variables at the very bottom of the page, below the closing body and html tags. Otherwise I don't see a problem. Unfortunately I'm having a lot of trouble getting it not to do that. Could it be this that's causing FF to have issues with the body?

Using colorbox's example files in Firefox does work (and the document.body is defined there).

Is there any way I could use jQuery to refill the document.body property with something from $() perhaps, or should I keep banging at drupal to not put a script tag outside the html tags (easier said than done)?

To clarify the document.body is null even after the page is done loading. Here's a Firebug console capture:

>>> document.body
null
>>> $().attr('body')
null
解决方案

The usual reason document.body is null is because you're executing script before the <body> tag(*) has been encountered. Without seeing code I can't say for sure, but that'd be the usual reason. Move where you've put the scripts.

(*: or another element such as <p> that the browser knows can't occur outside of a body, in which case it silently adds a <body> to fix up your missing markup. A difference in parsing of certain usually-in-body-tags might explain why Safari allows you to get away with it.)

No. It's broken markup and Drupal shouldn't do it, but closing the </body> doesn't stop document.body referencing the proper DOM Node.

No, document.body is only reflecting basically the same as document.getElementsByTagName('body')[0], so trying to set it is not sensible, and if you can't find a body with document.body, jQuery won't be able to find it with $('body') either.

这篇关于为什么在Firefox中document.body == null,而不是Safari的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 06:50
查看更多