English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Quick Solution to Get Empty style.left Value of Floating(float) Element in JS

Solution:

1.Set the top and left values of the element using inline styles;

2.or directly obtain the offsetLeft of the element to get the relevant value, and there is no need to use parseInt

Reason for the problem:

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 style.left of the child div is relative to the value of the parent div, which is the same as offsetLeft. The difference is that:

style.left returns a string, such as28px, offsetLeft returns a number28, it is more convenient to use offsetLeft for calculations.

style.left is readable and writable, while offsetLeft is read-only, so to change the position of div, you can only modify style.left.

The value of style.left needs to be defined in advance; otherwise, the value obtained will be empty, and it must be defined in the form of inline styles. If defined in css, the value of style.left is still undefined; while offsetLeft can still be obtained without defining the position of div in advance.

The above-mentioned is a quick solution introduced by the editor to solve the problem of getting the style.left value of floating (float) elements in JS as empty. I hope it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time!

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like