jQuery:为什么CSS(' width')返回的值与width()不同
问题描述:
当我运行以下代码时:
node = $('.period')
alert(node.width() + ' ' + node.css('width'))
我得到"0 144px".那怎么可能?
i get '0 144px'. How is that possible?
答
也许您是在$(document).ready上调用它的,并以 width() 的形式获取当前计算的,像素,第一个匹配元素的宽度.,由于尚未渲染,因此为0 ...
css('width')但是从css样式表中读取,该样式表已经可用.
Maybe you're calling it on $(document).ready, and as width() Gets the current computed, pixel, width of the first matched element., it'd be 0 as it has not been rendered yet...
css('width') however reads from the css stylesheet, which would be already available.