In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to solve the problem that you can't get the value through div.style.left in JavaScript". In the daily operation, I believe that many people have doubts about how to solve the problem that they can't get the value through div.style.left in JavaScript. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt that "how to solve the problem that you can't get the value through div.style.left in JavaScript." Next, please follow the editor to study!
1. Summary of the questions:
The style must be written directly inside the element to get the attribute value directly through div.style.left (that is, it must be an inline style), and styles defined in css cannot be obtained in this way.
Move the element to 200 to stop
SetTimeout (function () {var div = document.getElementById ("div4"); / / var left = parseInt (div.style.left) + 5; var left = div.offsetLeft + 5; div.style.left = left + "px"; if (left
< 200) { setTimeout( arguments.callee, 50); } }, 50); 二、关于offsetLeft和left的区别 1.定义了position:relative或absolute属性的元素才有left属性,元素都有offsetLeft属性。 2.元素内联样式中设置了left,才能通过div.style.left获取;offsetLeft直接通过div.offsetLeft获取。 3.left可读可写,获取到的是字符串;offsetLeft只读,获取到的是数字。 相同点:定位方式一样,如果父元素设置了定位元素(position设置为relative或absolute),则相对于定位元素定位,否则就是相对于根元素定位。 三、案例: HTMLElement.offsetParent 是一个只读属性,返回一个指向最近的(closest,指包含层级上的最近)包含该元素的定位元素。如果没有定位的元素,则 offsetParent 为最近 的 table 元素对象或根元素(标准模式下为 html;quirks 模式下为 body)。当元素的 style.display 设置为 "none" 时,offsetParent 返回 null。offsetParent 很有用,因为 offsetTop 和 offsetLeft 都是相对于其内边距边界的。 下面的span的父元素没有设置定位元素,所以该元素的offsetParent为根元素,此时的offsetTop相对于根元素定位。 Short span. Long span that wraps withing this div. var box = document.getElementById("box"); var long = document.getElementById("long"); // // long.offsetLeft这个值就是span的offsetLeft. // span是个行内元素,它没有没absolute定位,但还是默认offserParent就是父元素,而不是根 // box.style.left = long.offsetLeft + document.body.scrollLeft + "px"; box.style.top = long.offsetTop + document.body.scrollTop + "px"; box.style.width = long.offsetWidth + "px"; box.style.height = long.offsetHeight + "px";If you add a positioning attribute to the parent element of long, the result is as follows:
At this point, the study on "how to solve the problem that you can't get a value through div.style.left in JavaScript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.