相关疑难解决方法(0)

通过JavaScript访问CSS自定义属性(也称为CSS变量)

如何var(…)使用JavaScript(plain或jQuery)获取和设置CSS自定义属性(在样式表中访问的属性)?

这是我不成功的尝试:单击按钮更改常用font-weight属性,但不更改自定义--mycolor属性:

<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <style>
    body { 
      --mycolor: yellow;
      background-color: var(--mycolor);
    }
  </style>
</head>
<body>

  <p>Let's try to make this text bold and the background red.</p>
  <button onclick="plain_js()">Plain JS</button>
  <button onclick="jQuery_()">jQuery</button>

  <script>
  function plain_js() { 
    document.body.style['font-weight'] = 'bold';
    document.body.style['--mycolor'] = 'red';
  };
  function jQuery_() {
    $('body').css('font-weight', 'bold');
    $('body').css('--mycolor', 'red');
  }
  </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

javascript css jquery css-variables

31
推荐指数
3
解决办法
1万
查看次数

CSS伪元素计数器:你能增加一个字母"a","b","c"等而不是数字?

如此处所定义:

http://www.w3.org/TR/CSS21/generate.html#propdef-counter-increment

您可以使用以下代码来增加伪元素中的数字.

H1:before {
    content: "Chapter " counter(chapter) ". ";
    counter-increment: chapter;  /* Add 1 to chapter */
}
H1 {
    counter-reset: section;      /* Set section to 0 */
}
H2:before {
    content: counter(chapter) "." counter(section) " ";
    counter-increment: section;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法可以使用相同的代码来增加像"a","b","c"等字母?

谢谢!

css pseudo-element css-content

29
推荐指数
1
解决办法
2万
查看次数