为什么jQuery的height()自动舍入该值? [英] Why does jQuery's height() automatically rounds up the value?
问题描述
我想计算列表的计算高度.在IE上,这两个给出不同的结果.在Chrome上,该值似乎始终为整数,因此我没有遇到这个问题.
I wanted to calculate the computed height of a list. On IE, these two give different results. On Chrome, the value seems to be always on integer, so I do not get this problem.
// gives a string of "353.7px"
window.getComputedStyle(mylist, null).getPropertyValue("height")
// gives an int of 354
$(mylist).height();
jQuery如何删除小数,或者这是IE的问题?
How come jQuery drops decimals, or is this a problem with IE?
编辑我实际上是在撒谎. Chrome会发生这种情况.参见 http://jsfiddle.net/jTPk9/
EDIT I was actually lying. This happens with Chrome. See http://jsfiddle.net/jTPk9/
推荐答案
这是jQuery中的一个已知错误,不应将高度和宽度值取整.
This is a known bug in jQuery, which shouldn't round height and width values.
http://bugs.jquery.com/ticket/9628
已更新@CookieMonster的评论
当前的解决方法是使用.getBoundingClientRect().height
,它是跨浏览器并且不舍入值.
The current workaround is to use .getBoundingClientRect().height
, which is cross browser and doesn't round values.
document.getElementById("fruits").getBoundingClientRect().height
这篇关于为什么jQuery的height()自动舍入该值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!