为什么Firefox返回0作为$(window).height()的值 [英] Why does Firefox return 0 as the value of $(window).height()

查看:161
本文介绍了为什么Firefox返回0作为$(window).height()的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,当使用 $(window).height()时,Firefox应该会更好地装备它以返回正确的值。 b

我正在使用jQuery 1.8.2。这是特定版本中的错误吗?我测试过的每一个浏览器在计算视口高度时都返回一个合适的值。

为了解决这个问题,我使用了下面的代码将一个像素值应用到div。

$ $ $ $ $ $ $ $ $($) .height();
if(height === 0){
height = window.innerHeight;
}
height = height - '120';
$ ('#game_objects')。css('height',height +'px');
});


解决方案jQuery的$(window).height()是敏感的到doctype。试试<!doctype html> ?

jQuery 1.8.1发行说明


请勿使用怪癖模式! jQuery从来不支持怪异模式,我们不在Quirks中进行任何测试。这可以影响像$(window)。height()这样的值,而jQuery 1.8 Quirks模式结果确实改变了,以支持一些现代浏览器功能。我们看到的大多数问题都来自开发人员,他们希望进入标准模式,但在标签之前有一个无效的文档类型或无关的标记。如果有疑问,请使用简单的<!doctype html>。

例如,使用jQuery 1.8.2和Firefox 16 ,我得到一个有效的$(窗口).height()doctype html,但高度0与doctype html5。在铬20,都工作。 (W3C的HTML5规范没有说使用doctype html,而不是html5。)


It seems to me that Firefox ought to be better equipped to return the proper value when using $(window).height().

I am using jQuery 1.8.2. Is this a bug in that particular build? Every other browser I have tested returns an appropriate value when calculating viewport height.

To work around this, I've used snip below to apply a pixel value to a div.

$(window).bind('cwsObjectsShown', function() {
  var height = $(window).height();
  if (height === 0) {
     height = window.innerHeight;
  }
  height = height - '120';
  $('#game_objects').css('height', height + 'px');
});

解决方案

jQuery's $(window).height() is sensitive to doctype. Try <!doctype html> ?

The jQuery 1.8.1 release notes say

Don’t use Quirks mode! jQuery has never supported Quirks mode and we do not perform any testing in Quirks. This can affect values like $("window").height(), and the jQuery 1.8 Quirks mode results did change in order to support some modern browser features. The majority of problem cases we’ve seen are from developers that wanted to be in Standards mode but had an invalid doctype or extraneous markup before their tag. When in doubt, use the simple and short <!doctype html>.

For example, with jQuery 1.8.2 and Firefox 16, I get a valid $(window).height() with doctype html, but height 0 with doctype html5. In Chromium 20, both work. (The W3C HTML5 spec does say to use doctype html, not html5.)

这篇关于为什么Firefox返回0作为$(window).height()的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆