getComputedStyle 属性 值

getComputedStyle property values

我正在尝试报告我创建的椭圆 div 的边界半径值,但我得到了一个未定义的返回值。谁能解释为什么?我是犯了一个简单的错误还是我的代码有问题?谢谢!

<!DOCSTYLE html>
<html>
<head>
    <title>CSS3</title>
    <style>
        #oval{
            width: 500px;
            height: 300px;
            background: black;
            border-bottom-left-radius: 50%;
            border-bottom-right-radius: 50%;
            border-top-left-radius: 50%;
            border-top-right-radius: 50%;
        }
    </style>
    <script>
        var myOval = document.getElementById('oval');
        var bRadBL = window.getComputedStyle(myOval).getPropertyValue("border-bottom-left-radius");
        var bRadBR = window.getComputedStyle(myOval).getPropertyValue("border-bottom-right-radius");
        var bRadTL = window.getComputedStyle(myOval).getPropertyValue("border-top-left-radius");
        var bRadTR = window.getComputedStyle(myOval).getPropertyValue("border-top-right-radius");
        var bRad = getComputedStyle(myOval).getPropertyValue("borderRadius");
        function compStyle(){
            alert("Top Left Radius: "+bRadTL+"\nBottom Left Radius: "+bRadBL+"\nTop Right Radius: "+bRadTR+"\nBottom Right Radius: "+bRadBR);
     }
    </script>
</head>
<body>
    <div id="oval"></div>
    <input type="button" value="click me" onClick="compStyle()"/>
</body>
</html>

编辑:我尝试了 "border-bottom-left-radius" 和 "borderBottomLeftRadius",结果相同。我应该使用哪一个?

您 运行 呈现元素之前的脚本。将脚本块移动到主体的末尾,在 html 元素 id 声明之后:

#oval{
  width: 500px;
  height: 300px;
  background: black;;
  border-bottom-left-radius: 50%;
  border-bottom-right-radius: 50%;
  border-top-left-radius: 50%;
  border-top-right-radius: 50%;
}
<div id="oval"></div>
<input type="button" value="click me" onClick="compStyle()"/>

<script>
  var myOval = document.getElementById('oval');
  var bRadBL = window.getComputedStyle(myOval).getPropertyValue("border-bottom-left-radius");
  var bRadBR = window.getComputedStyle(myOval).getPropertyValue("border-bottom-right-radius");
  var bRadTL = window.getComputedStyle(myOval).getPropertyValue("border-top-left-radius");
  var bRadTR = window.getComputedStyle(myOval).getPropertyValue("border-top-right-radius");
  var bRad = getComputedStyle(myOval).getPropertyValue("borderRadius");
  function compStyle(){
    alert("Top Left Radius: "+bRadTL+"\nBottom Left Radius: "+bRadBL+"\nTop Right Radius: "+bRadTR+"\nBottom Right Radius: "+bRadBR);
  }
</script>