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

Common ways to get styles in Js

Nov 28, 2017 pm 03:26 PM
javascript common Way

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