简体   繁体   中英

I can't get the width of a div in javascript

My code positions a div where my mouse is at the time and is supposed to move the div to the left the same amount as its width if it is too close to the right side of the page. But I can't seem to get the width of the div. Here is my code:

function position(e) {
  document.getElementById(tt).style.visibility = "visible";
  if (document.body && document.body.offsetWidth) {
    var winW = document.body.offsetWidth;
    var winH = document.body.offsetHeight;
  }
  if (e.pageX || e.pageY) {
    l = e.pageX;
    u = e.pageY;
  } else if (e.clientX || e.clientY) {
    l = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
    u = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  }
  var divL = document.getElementById(tt).style.width;
  if ((winW - l) <= 320) {
    l = l - divL;
  }
  document.getElementById(tt).style.top = (u + 10) + 'px';
  document.getElementById(tt).style.left = (l + 10) + 'px';

}
var width = document.getElementById(tt).offsetWidth;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM