Will jQuery ever return a CSS measurement in something other than px?
I'm rewritting my Textarea Line Count plugin (<-- shameless plug), and have this question:
When I call $("#someElement").css("letter-spacing")
, will I ever get a value in em
s, or anything other than px
? Based on this examp开发者_开发技巧le: http://jsfiddle.net/xhhr2/, in Google Chrome at least, it appears that either jQuery or the browser is converting the measurement to px
for me. Can I always expect this behavior?
According to Crescent Fresh's answer that links to a hack by Dean Edwards, jQuery goes through great lengths to return the actual, computed pixel value across all browsers and not what was defined originally in the style sheet, so it seems that yes, you can rely on it.
As far as I know, the browser has to convert every size you give it (whether it's in em
, %
. etc) into pixels. That's how the DOM stores it, and thus jQuery will return that value to you.
EDIT
According to this answer here, (referenced by Pekka's answer) only IE supports the currentStyle
, which gives the size (including the unit) that was set by CSS. Apparently, no other browser supports this, instead they all use computed style
which converts everything into pixels. So I was about half-right. :P
精彩评论