首页javascriptwidthJavascript Style - 如何返回width的样式属性

Javascript Style - 如何返回width的样式属性

我们想知道如何返回width的样式属性。

<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#foo {
  background: green;
  width: 80px;
}
</style>
<script type='text/javascript'>
function getWidth() {
    var elem = document.getElementById("foo");
    var theCSSprop = window.getComputedStyle(elem, null).getPropertyValue("width");
    console.log(theCSSprop);
}

</script>
</head>
<body>
  <div id="foo" onClick="getWidth()">Hello World. click and check the console.</div>
</body>
</html>