js习题(获取伪元素属性的方法)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>中国的流浪猫</title>
    <style>
        div {
     
     
            width: 500px;
            height: 500px;
            background-color: red;
        }

        div::after {
     
     
            content: '';
            display: inline-block;
            width: 200px;
            height: 100px;
            background-color: yellow;
        }
    </style>
</head>

<body>
    <div></div>
</body>

</html>
<script>
    var div = document.querySelector('div')
    //第二个参数填伪元素,要字符串形式
    console.log(getComputedStyle(div, 'after').backgroundColor);
    console.log(getComputedStyle(div, 'after').width);
    console.log(getComputedStyle(div, 'after').height);
</script>

猜你喜欢

转载自blog.csdn.net/weixin_46611729/article/details/108844694