jQuery .css(“left”)返回“auto”而不是Chrome中的实际价值 [英] jQuery .css("left") returns "auto" instead of actual value in Chrome

查看:357
本文介绍了jQuery .css(“left”)返回“auto”而不是Chrome中的实际价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义了left和top的div元素,没有绝对位置,我想使用jQuery读取左侧和顶部的值。

I have div element with left and top defined, without absolute position, and I want to read the left and top values using jQuery.

在IE浏览器(IE8)中使用 $(#MyId)。css(left)给出预期结果,

Using $("#MyId").css("left") gave the expected result in IE browser (IE8) but in Chrome it returned "auto" instead, although the values are explicitly written in the element style.

这里是测试用例:
http://jsfiddle.net/qCDkb/2/

请注意IE和Chrome之间的区别。

Note the difference between IE and Chrome.

此外,这在jQuery 1.4.2和失败在jQuery 1.4.3及以上工作良好。

Also, this is working well in jQuery 1.4.2 and "failing" in jQuery 1.4.3 and above.

欢迎。 : - )

推荐答案

如注释中所述,将 left 设置为自动位置:静态听起来不错,看到

As discussed in the comments, setting left to auto for a position: static sounds somehow right, seeing as left has no meaning in the context.

为什么Chrome和IE会返回不同的值: .css()为浏览器计算的样式函数提供了一个统一的网关,但它不统一浏览器实际计算样式的方式。浏览器不太常见的决定这种边缘情况不同。

As to why Chrome and IE return different values: .css() provides a unified gateway to the browsers' computed style functions, but it doesn't unify the way the browsers actually compute the style. It's not uncommon for browsers to decide such edge cases differently.

至于为什么jQuery 1.4.2和1.4.3做不同的,我不知道肯定,但 1.4.3的发行说明中有以下内容:

As to why jQuery 1.4.2 and 1.4.3 do this differently, I do not know for sure, but there's this in 1.4.3's release notes:


几乎整个CSS模块已经重写,完全基于可扩展性。现在,您可以编写自定义CSS插件,扩展由.css()和.animate()提供的功能。

Nearly the entire CSS module has been rewritten focusing entirely on extensibility. You can now write custom CSS plugins that extend the functionality provided by .css() and .animate().

这篇关于jQuery .css(“left”)返回“auto”而不是Chrome中的实际价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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