【发布时间】:2016-09-25 13:13:40
【问题描述】:
有什么方法可以根据类的子编号来自动化伪元素的内容值吗?我希望这是有道理的。
例如,我的列表中有 10 个项目,然后我必须使用 li:nth-child:before将它们在列表中的位置放入 content 我必须简单地放入 1、2、3、4、5...依此类推,直到最后一项被声明。如果列表有 100 个项目,这是不合适的。
有什么办法只能在 CSS 上实现吗?如果可能的话,javascript 或 jquery 可能足以提供正确的解决方案。
HTML
CSS
ul.numeric {float:left; width:100%; list-style-type:none; position:relative;}
ul.numeric li {position:relative; padding-left:40px; line-height:40px;}
ul.numeric li:after { content: ''; position: absolute; display: inline-block; top: 0; left: 3px; line-height: 32px; width: 32px; height: 32px; padding: 0; background: url(/Images/star-yellow.png) left center no-repeat; background-size: 100%; font-size: 18px; text-align: center; color: #565656; font-family: Georgia, sans-serif;}
ul.numeric li:first-child:after {content:'1';}
ul.numeric li:nth-child(2):after {content:'2';}
ul.numeric li:nth-child(3):after {content:'3';}
ul.numeric li:nth-child(4):after {content:'4';}
ul.numeric li:nth-child(5):after {content:'5';}
ul.numeric li:nth-child(6):after {content:'6';}
基本上,当前设置只能提供正确的内容值,直到第 6 项。
Fiddle 可用here
【问题讨论】:
标签: javascript jquery html css