首页javascriptclassJavascript Style - 如何使用!important标志通过DOM设置CSS属性

Javascript Style - 如何使用!important标志通过DOM设置CSS属性

我们想知道如何使用!important标志通过DOM设置CSS属性。

<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#test {
  padding: 1em;
  font-size: 50px;
  font-weight: bold;
  color: red !important;
}
</style>
</head>
<body>
  <div id="test">This is a test</div>
  <script type='text/javascript'>
var elem = document.getElementById('test');
elem.style.setProperty( 'color', 'green', 'important' );

</script>
</body>
</html>