Understanding about offsetleft compatibility and offsetleft compatibility
For details about the basic usage of this attribute, refer to the offsetleft attribute usage section.
This attribute has certain compatibility issues, that is, in the IE 7 browser, it returns the distance to the left of the most recent parent element.
<! DOCTYPE html>
The above code returns 100 in other browsers, but in IE 7, the return value is 50.
IE6 is not tested. If you are interested, you can perform the test.
The following example shows the differences between offsetLeft and style. left.
OffsetLeft obtains the left margin relative to the parent object.
Left gets or sets the left margin relative to the parent object with the position attribute (position defined as relative ).
If the position of the parent div is defined as relative and the position of the child div is defined as absolute, the style. left value of the Child div is relative to the parent div value,
This is the same as offsetLeft. The difference is:
1. style. left returns a string, such as 28px, and offsetLeft returns a value of 28. If you need to calculate the obtained value,
It is also convenient to use offsetLeft.
2. style. left is read/write, and offsetLeft is read-only. To change the position of the div, you can only modify style. left.
3. The value of style. left must be defined in advance. Otherwise, the obtained value is null. And must be defined in html. I have tried it. If it is defined in
In css, the value of style. left is still empty, which is the problem I encountered at the beginning. The value of style. left is always not available.
OffsetLeft can still be obtained without the need to define the position of the div in advance.