如何获取:: before伪元素的文本内容?

时间:2015-10-10 22:11:26

标签: javascript css pseudo-element getcomputedstyle

在此示例中,:: before伪元素的内容不是静态值,而是计数器。我想获得当前价值" 2"而不是通用公式" counter(itemcounter)"。



// Expected value is 2 instead of counter(itemcounter).
alert(window.getComputedStyle(document.querySelector('li:nth-child(2)'), '::before').getPropertyValue('content'));

ul {
  counter-reset: itemcounter;
  list-style-type: none;
}
li {
  line-height: 20px;
  margin-bottom: 10px
}
li::before {
  background: #000;
  border-radius: 10px;
  color: #fff;
  counter-increment: itemcounter;
  content: counter(itemcounter);
  display: block;
  float: left;
  font-family: sans-serif;
  font-weight: 700;
  font-size: 14px;
  height: 20px;
  line-height: 20px;
  margin-right: 5px;
  text-align: center;
  width: 20px;
}

<ul>
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
</ul>
&#13;
&#13;
&#13;

0 个答案:

没有答案