Description of the usage of style.left and offsetLeft in javascript

  • 2020-03-30 02:19:26
  • OfStack

If the position of the parent div is defined as relative and the position of the child div is defined as absolute, then the value of styli. left of the child div is relative to the value of the parent div, which is the same as that of 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 value obtained, it is more convenient to use offsetLeft.

2. Style. left is read and write, offsetLeft is read only, so to change the div position, you can only modify style.left.

3. The value of style.left needs to be defined in advance, otherwise the value will be null. And you have to define it in HTML, and I've experimented with it, and if you define it in CSS, the value of style.left is still empty, and that's the problem that I had at the beginning, where you never get the value of style.left.

OffsetLeft can still be fetched without having to define the location of the div.

// this function is the operation of a drop-down box of infinite classification. There is only one drop-down box at the beginning of the page.
Generate a select dynamically. Select items are subcategories, and move the select box of the subcategory back 20px.


function itemtree_cats_change ( selectObj )


Related articles: