我尝试使用 javascript 访问 div 的宽度,但无论我设置什么,它每次都打印 NaN。我是 javascript 新手,我的代码有什么问题?
<html>
<head>
<title>hello</title>
<style type="text/css">
#base
{
width:300px;
height:30px;
background-color: black;
}
#scr
{
height:30px;
width:10px;
background-color: red;
}
</style>
<script type="text/javascript">
var magic = function()
{
console.log("inside magic")
var d = document.getElementById("scr");
var b = d.style.width;
console.log(b);
b = parseInt(b);
console.log(b);
}
</script>
</head>
<body>
<div id="base">
<div id = "scr" >
</div>
</div>
<br>
<button onclick="magic()">Magic</button>
</body>
</html>