Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.


今天复习了一下关于getComputedStyle的用法,遇到了小小的挫折。报错前的代码:

<div class="box">
        <div id="box1">
            <input type="text">
            <input type="text">
            <input type="text">
        </div>
    </div>
    <style>
        .box{
            width: 300px;
            height: 200px;
            margin: 20px auto;
            border: 1px solid red;
        }
        #box1{
            width: 100%;
            height: 100px;
            background:blue;
            margin: 5px auto;
        }
    </style>
    <script>
        window.onload=function(){
            var box = document.getElementsByClassName('box');
            alert(window.getComputedStyle(box,null).height)
        }
    </script>

 

当然,这只是随便举的例子,然而却遇到了标题上面的报错,纠结了一会后,终于解决了。原来是获取dom元素时,应该通过标签中的id属性去获取,这样仅需要改为

var box = document.getElementById('box1')就OK啦,是不是很简单。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM