首页javascriptclassJavascript Style - 如何切换样式类on/off css选择器

Javascript Style - 如何切换样式类on/off css选择器

我们想知道如何切换样式类on/off css选择器。

<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.on {
  background-color: red;
}
</style>
</head>
<body>
  <table onclick="this.classList.toggle('on');">
    <tr>
      <td>123</td>
    </tr>
  </table>
</body>
</html>