简体   繁体   English

根据list-style-type CSS获取索引号对应的字符

[英]Get character corresponding to index number based on list-style-type CSS

https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_ol_type_all_css https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_ol_type_all_css

I want to extract the correct character based on the list-style-type and numerical index in CSS我想根据 CSS 中的列表样式类型和数字索引提取正确的字符

For example, if I want the character for 3 in list-style-type: lower-greek;例如,如果我想要list-style-type: lower-greek; : γ : γ

Example 2, I want the character for 9 in list-style-type: cjk-ideographic;示例 2,我想要list-style-type: cjk-ideographic; : 九: 九

Example 3, I want the characters 1,2,3 in list-style-type: lower-roman;示例 3,我想要list-style-type: lower-roman; : i,ii,iii : 一、二、三

is there a way to do this easily with code?有没有办法用代码轻松做到这一点?

I believe characters for 3 would have an index of 2, and characters for 9 would have an index of 8... since indexing starts at 0.我相信 3 的字符的索引为 2,而 9 的字符的索引为 8 ......因为索引从 0 开始。

How do I return the correct character(s) for the number(s) I want, based on list-style-type?如何根据列表样式类型为我想要的数字返回正确的字符?

First Answer (CSS but no JS)第一个答案(CSS但没有JS)

I believe you will have to use the n-th child selector , combined with list-style-type: 'γ. ';我相信您将不得不使用第n 个子选择器,并结合list-style-type: 'γ. '; list-style-type: 'γ. '; ( mozilla docs )! Mozilla 文档)!

Checkout @ codepen: https://codepen.io/netrules/pen/yLzBMqK结帐@codepen: https://codepen.io/netrules/pen/yLzBMqK

<ul>
  <li>a, b, c, d</li>
  <li>e, f, g, h</li>
  <li>i, j, k, l</li>
  <li>...</li>
  <li>now i know my abc's!</li>
</ul>
ul {
  list-style-type: lower-greek;
}

li:nth-child(3) {
  list-style-type: 'γ. ';
}

I don't know if there is a better workaround though.我不知道是否有更好的解决方法。 I will check out again for future comments.我会再次检查以获取未来的评论。

Second Answer第二个答案

Upon second reading, I think what you might be referring to, is actually something similar to what's displayed in this css-tricks post .在二读时,我认为您所指的实际上与此css-tricks 帖子中显示的内容相似。 You can look up the pre-defined counter styles in this website and then write the corresponding javascript to evaluate and map them.您可以在本网站查找预定义的计数器 styles,然后编写相应的 javascript 来评估和 map。 Also relevant: unit tests for CSS3 Counter Styles in relation to browser kits [ url ].也相关:与浏览器套件相关的 CSS3 计数器 Styles 的单元测试 [ url ]。

Here's one way to do it: https://codepen.io/netrules/pen/KKXPWEo这是一种方法: https://codepen.io/netrules/pen/KKXPWEo

const charmap = "900 ϡ, 800 ω, 700 ψ, 600 χ, 500 φ, 400 υ, 300 τ, 200 σ, 100 ρ, 90 ϟ, 80 π, 70 ο, 60 ξ, 50 ν, 40 μ, 30 λ, 20 κ, 10 ι, 9 θ, 8 η, 7 ζ, 6 στ, 5 ε, 4 δ, 3 γ, 2 β, 1 α";
const processedChars = charmap.split(", ");
const mapArray = processedChars.map(x => {
  let vals = x.split(" ");
  return {rem:vals[0], symbol:vals[1]};
})

function evalNumberToListKey(num) {
  let numCalc = num;
  let out = '';
  for(let element of mapArray) {
    const temp = numCalc%element.rem;
    if(temp !== numCalc) {
      numCalc = temp;
      out += element.symbol;
    }
  }
  return out;
}

document.write(evalNumberToListKey(951));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM