简体   繁体   English

如何获取JavaScript中元素的宽度

[英]How to get width of element in javascript

I am trying to get the width and height of an element. 我正在尝试获取元素的宽度和高度。 My element is a DIV with the id of "page". 我的元素是ID为“ page”的DIV。

The current code is not working as my div has a border and it allows my button to go out of the border. 当前代码不起作用,因为我的div有边框,并且允许我的按钮移出边框。

I have tried the .offsetWidth as well as .clientwidth - Both returned null values. 我已经尝试过.offsetWidth以及.clientwidth-两者都返回null值。

var buttonState = document.getElementById("clickMe");
var maxArea = document.getElementById("page");
var pageWidth = maxArea.width;
var pageHeight = maxArea.height;
var screenWidth = 0;
var screenHeight = 0;
var buttonSize = 4;

的document.getElementById( “页”)。style.width

window.getComputedStyle("page").getPropertyValue('font-size');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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