getComputedStyle获取元素样式值

html:

    <style>
      .div {
        width: 200px;
        height: 200px;
        background-color: greenyellow;
      }
    </style>
  </head>
  <body>
    <div class="div">一些文字</div>
    <script>
      const div = document.getElementsByClassName('div')[0]
      const style = getComputedStyle(div)
      console.log(style)
      console.log(style.backgroundColor) // rgb(173, 255, 47)
    </script>
  </body>

vue:

  mounted() {
    const ref = this.$refs.h1Ref
    const style = getComputedStyle(ref)
    console.log(style)
    console.log(style.color)
    ref.classList.add('aa')
  }

 

上一篇:JavaScript 字符串拼接 & setInterval()实现简单动画


下一篇:LeetCode题解(十)0900-0999,android实现文件下载