-1

所以我希望这个VarDisk2元素在宽度达到70以上后消失VarDisk1。我该怎么做?

 <script>
        var VarDisk1 = document.querySelector("Disk1");
        var VarDisk2 = document.getElementsByClassName("Disk2");
        let width = VarDisk1.offsetWidth;

        if(width > 70)
            VarDisk2.style.display = "none";

    </script>
4

1 回答 1

1

用于检查所有类

<script>
        ...
        var VarDisk2 = document.getElementsByClassName("Disk2");
        let width = VarDisk1.offsetWidth;

        if(width > 70) {
            VarDisk2.forEach(ele => ele.style.display = "none";)
        }

</script>

头等舱:

<script>
        ...
        var VarDisk2 = document.getElementsByClassName("Disk2")[0];
        ...
</script>
于 2021-09-01T11:05:39.200 回答