This is the test1 DIV.
It has these styles:
	width: 300px;
	height: 200px;
	border: 10px solid #666;
	padding: 5px;
	overflow: hidden;

Its total visible width and height are therefore 330 pixels (300 width + 10 padding + 20 border).


This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test2 DIV.
It has these styles:
	width: 300px;
	height: 200px;
	border: 10px solid #666;
	padding: 5px;
	overflow: auto;

在ie6和ie7中,clientWidth包含滚动条的宽度; 在其它A级浏览器以及ie8 beta1中,clientWidth减去了滚动条的宽度


This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test DIV.
This is the test3 DIV.
It has these styles:
	width: 300px;
	height: 200px;
	border: 10px solid #666;
	padding: 5px;
	overflow: auto;

在ie6和ie7中,clientWidth == 0
解决办法是通过zoom: 1等方式使得hasLayout为true
注意:此时offsetWidth有值

This is the test4 DIV.
It has these styles:
	width: 300px;
	height: 200px;
	border: 10px solid #666;
	padding: 5px;
	overflow: auto;
	zoom: 1;

#test4和#test5样式完全一样,
但在ie7中,clientWidth的值不同。
当zoom: 1遭遇overflow时,ie7很诡异

This is the test5 DIV.
It has these styles:
	width: 300px;
	height: 200px;
	border: 10px solid #666;
	padding: 5px;
	overflow: auto;
	zoom: 1;

#test4和#test5样式完全一样,
但在ie7中,clientWidth的值不同。
当zoom: 1遭遇overflow时,ie7很诡异

#specialTest1 测试offsetHeight

Summary

  1. clientWidth and clientHeight的定义:The width and height of the content field, excluding border and scrollbar, but including padding. 但在ie6和ie7中,没有excluding scrollbar.
  2. 在ie6和ie7中,当元素的hasLayout为false时,clientWidth和clientHeight始终为0
  3. 在ie7中,zoom为1时,clientWidth有时依旧为0(参考test4和test5)
  4. 总之,在ie6和ie7中,某些情况下(很难一一找出),clientWidth会为0,是UNRELIABLE的。
  5. 相对而言,offsetHeight要可信得多。但是看上面的test6,在ie7中也挂了。

References

  1. http://msdn.microsoft.com/en-us/library/ms533566.aspx
  2. http://www.quirksmode.org/dom/w3c_cssom.html#elementview
View Comments