Home > Web Front-end > JS Tutorial > Common ways to get styles in Js

Common ways to get styles in Js

php中世界最好的语言
Release: 2017-11-28 15:26:46
Original
2618 people have browsed it

We know that if you want to operate CSS in JS, the most important thing is to obtain the style. Here is an article about the most common methods of obtaining styles in JS for your reference.

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    div{
      color:yellow;
    }
  </style>
</head>
<body>
  <div style="width:100px;height:100px;background-color:red">This is div</div>
</body>
</html>
Copy after login

Get it by using the element.style property

<script>
  var div = document.getElementsByTagName("div")[0];
  console.log(div.style.color); //""
  console.log(div.style.backgroundColor); //red
</script>
Copy after login

The element.style property can only get the inline style, not the style in the