为什么警告窗口不显示按钮的宽度?我浏览了类似的问题,但看起来我的问题太简单了,无法找到答案。
<!DOCTYPE html>
<html>
<body>
<button id="jock" onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
alert(document.getElementById("jock").style.width);
}
</script>
</body>
</html>
最佳答案
使用offsetWidth代替样式宽度。这将获得元素的真实宽度值。
<!DOCTYPE html>
<html>
<body>
<button id="jock" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
alert(document.getElementById("jock").offsetWidth);
}
</script>
</body>
</html>
关于JavaScript:getElementById 不返回按钮的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45129821/