2011-06-28 36 views
3

我们只是花了一些时间在IE7/8中追寻一些非常奇怪的jQuery/css行为。jquery css(空白)IE7/8的bug?

我们做一些动画和高度测量。虽然动画我们不希望文字换行。所以我们做了:

$(selector).css("white-space", "nowrap"); 
... animate ... 
$(selector).css("white-space", "inherit"); 

这个伟大的工程在FF,Chrome浏览器,Safari浏览器的iPad,甚至IE9,但在IE7或8,我们得到一个JavaScript错误:

"Could not get the whiteSpace property. Invalid argument" 

深在的肠子jQuery的。有些挠头之后,我们终于实现了这一点:

首先,在的CSS定义一个类:

/* Kludge to avoid IE7/8-jQuery css('white-space') problem? */ 
.nowrap { white-space: nowrap; } 

然后修改上面的动画代码:

$(selector).addClass("nowrap"); 
... animate ... 
$(selector).removeClass("nowrap"); 

现在每个人都开心,但kludge真的让我感到困扰。 jQuery是否为IE做了某种CSS翻译?我们在jQuery 1.4.2上。

回答

3

测试这里:http://jsfiddle.net/nL48C//http://fiddle.jshell.net/nL48C/show/light/
的错误是在有IE7和IE8在[IE7]兼容模式。 它在IE8标准模式下工作正常。

注释掉inherit行:http://jsfiddle.net/nL48C/1//http://fiddle.jshell.net/nL48C/1/show/light/
的错误消失。

我知道IE7几乎完全doesn't support inherit,所以这是问题的根源。

但是,如果你尝试inherit使用jQuery 1.4.4,没有错误:
http://jsfiddle.net/nL48C/2//http://fiddle.jshell.net/nL48C/2/show/light/

它看起来像jQuery 1.4.2缺少了一些魔法1.4.4已解决此问题。

的jQuery 1.4.2

if (set) { 
    style[ name ] = value; 
} 

的jQuery 1.4.4

// Wrapped to prevent IE from throwing errors when 'invalid' values are provided 
// Fixes bug #5509 
try { 
    style[ name ] = value; 
} catch(e) {} 

解决方案:

  • 不要将其设置为inherit,将其设置为normal,这是initial value