怎么实现元素ol的降序排序显示

时间:2023-03-09 19:43:46
怎么实现元素ol的降序排序显示

  首先介绍一下什么是ol元素。这里直接引用MDN里面的定义:The HTML <ol> Element (or HTML Ordered List Element) represents an ordered list of items.也就是说这个元素的包含的li元素是带有数字序号的。为了更好阐述下面介绍的几种方法,我们首先写出一个有序列表:

 <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ol reversed</title>
</head>
<body>
<ol>
<li>item1: I'm a boy</li>
<li>item2: I'm a boy too</li>
<li>item3: Are you kidding me?</li>
</ol>
</body>
</html>

  看到的效果就是:

怎么实现元素ol的降序排序显示

  到这里我们就发现一个神圣的有序列表产生了。

  接下来就是介绍怎么实现元素ol的逆向排序显示了。实现的方法是很多的,在这里介绍三种方法,如果您有更好的方法,欢迎在下面的评论中提出。这里提到的三种方法分别使用HTML、CSS和JavaScript实现,样样都有,随你挑。

  首先介绍一下HTML的实现方法,这个办法应该是最简单有效的,因为在HTML5中已经为这个标签添加了一个属性:reversed,方法也很简单,只需要在ol元素的起始标签中添加一个 reversed属性,就能非常容易实现元素ol的降序排序显示,我们来看一看效果:

 <ol reversed>
<li>item1: I'm a boy</li>
<li>item2: I'm a boy too</li>
<li>item3: Are you kidding me?</li>
</ol>

怎么实现元素ol的降序排序显示

  生活就是这么容易,只需要一个单词就实现了我们想要的效果。当然这个方法也是有所限制的,并不是所有的浏览器都是支持的,在这里同样盗用MDN的一张图来表达博主的意思:

怎么实现元素ol的降序排序显示

  需要注意的是,上面的表格虽然现实Opera不支持reversed属性,但是博主用自己电脑上的opera24打开的时候,是能够显示倒序效果的,而IE浏览器,正如表格中显示的一样,直到IE11,仍然不支持这个属性。

  接下来介绍的就是用CSS实现的ol元素降序排序显示了。这里的实现思路是这样的:

  1. 不显示系统默认的list-style
  2. 利用伪元素 :before在每个li标签前面添加数字

  具体的技术细节我们等会儿再分析,直接干脆利落的上代码:

 <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ol reversed</title>
<style type="text/css">
ol.reverse {
list-style: none;
counter-reset: reverse 4;
}
ol.reverse > li:before {
content: counter(reverse) '.';
display: block;
position: absolute;
left: -30px;
text-align: right;
width: 20px;
}
ol.reverse > li {
counter-increment: reverse -1;
position: relative;
}
</style>
</head>
<body>
<ol class = "reverse">
<li>item1: I'm a boy</li>
<li>item2: I'm a boy too</li>
<li>item3: Are you kidding me?</li>
</ol>
</body>
</html>

  通过上面的实现方式,经过博主的检测,能够在Chrome37、opera24、IE8~IE11、FireFox32、Safari5.1中得到很好的显示效果。

怎么实现元素ol的降序排序显示

  现在就开始解析这个实现过程。首先我们看到了几个即便是有着相对丰富的CSS经验的开发者也未必见过的属性:counter-reset 、counter-increment。这个两个属性主要是和CSS counters是一家。什么是css counters呢?在CSS2.1的规范中介绍了一种新技术,允许开发人员使用伪类:after:before或者伪元素::after::before给任何元素创建自动递增计数器——类似于列表中的项目符号(list-style-type)。也就是说,通过这个属性我们能够自定义所有元素设置计数器,那么ol元素自然也就不在话下。这三个属性必须是配合使用的, 说得更加准确一点是,要使用好CSS counters,就需要组合下面五个属性:

counter-reset:

  语法规则:counter-reset:[ <identifier> <integer>? ]+ | none | inherit 其中identifier用来定义计数器的名称,这个名称可以自定义。integer此值用来设置调用计算数器时起始值,默认值为0,在上面的例子中我们自定义了一个计数器reverse,起始值为4

counter-increment

  语法规则:counter-increment:[ <identifier> <integer>? ]+ | none | inherit 其中identifier计数器名称,就调用counter-reset声明的计数器的标识符,integer主要用来预设递增的值,如果取值为负值时,表示递减。默认值为1。在上面的例子中,我们调用了用counter-reset声明的计数器reverse,并且预设递减的值为1。

content

  content是和伪类:before:after或者伪元素::before::after配合在一起使用,主要功能用来生成内容。

counter

  counter()是一个函数,其主要配合content一起使用,counter()函数接受两个参数,而且两参数之间使用逗号(,)来分隔。第一个参数是counter-increment定义的属性值<identifier>,用来告诉该文档插入的计数器标识符名称是什么。第二个是用来设置计数器的风格,有点类似于list-style-type。默认情况之下取值为十制。在上面的例子中,我们只是传递了一个参数reverse,那么计数器的风格就是十进制。

伪元素 :before等

  **:before**、**:after**或**::before**、**::after**:配合content用来生成计数器内容。

(为了更好地理解CSS counters,博主推荐大家好好看着这个:http://www.w3cplus.com/css3/css-counters.html,讲解的非常清楚)

  到这里,为什么上面的代码能够实现逆序排序ol的列表也就非常清楚了。不过,为了严谨,我们仍然需要注意兼容性的问题。通过查阅MDN,博主得到了浏览器对于counter-reset和counter-increment的支持情况:

怎么实现元素ol的降序排序显示

  那么我们能够下一个结论:这种方法能够在五大主流浏览器中得到比较好的支持,但是由于IE8还占有不小的市场份额,如果对于兼容性比较苛刻,需要谨慎考虑这种方法。

  最后,就要介绍杀手级的方法了,其实这也不算一种新的方法,只不过兼容不支持reversed的浏览器。我们上面提到过直到IE11还是不支持reversed属性,所以为了能够使reversed属性能够在IE浏览器中使用,就需要采用Javascript来解决了。方法其实也是比较简单的:

  (1)判断ol对象有没有reversed属性

  (2)如果没有reversed属性,给ol元素的直接li节点设置value属性。

  你可以在你的页面中引入下面的脚本,这样在IE8~IE11中你也能够使用reversed属性了。这段代码不是博主所写,有兴趣的可以在github上面查看源码:https://github.com/impressivewebs/HTML5-Reverse-Ordered-Lists/blob/master/js/script.js

 if (!('reversed' in document.createElement('ol'))) {

     (function () {
'use strict';
// Run the code on each ordered list with a "reversed" attribute.
var lists = document.getElementsByTagName('ol'),
length = lists.length,
i,
j,
child,
currChildren,
childrenLength,
start,
currCount,
val; for (i = 0; i < length; i += 1) { child = lists[i]; if (child.getAttribute('reversed') !== null) { currChildren = child.getElementsByTagName('li');
childrenLength = currChildren.length;
start = child.getAttribute('start'); // Check the existence of the start attribute and if it's
// a number.
if (start !== null && !isNaN(start)) {
currCount = start;
} else {
// Do this if the start attribute is not present. The first
// number is derived from the number of list items.
// (Ugh; Do we need double loops to get the correct count?) currCount = 0; for (j = 0; j < childrenLength; j += 1) { if (currChildren[j].parentNode === child) {
currCount += 1;
} }
}
// Go through each list item, adding the 'value' attribute
// plus currCount number then subtract one from currCount
// so we're ready for the next one.
for (j = 0; j < childrenLength; j += 1) { if (currChildren[j].parentNode === child) {
// Per spec, if set, the value attribute should be used
// and renumbering started from there.
val = currChildren[j].getAttribute('value'); if (val !== null && !isNaN(val)) {
currCount = val;
} currChildren[j].setAttribute('value', currCount);
currCount -= 1;
}
}
}
} }()); }

  那么,有关如何怎么实现元素ol的降序排序显示,就大概讲解到这里了。如果您希望转载本文,请注明转载地址:http://www.cnblogs.com/yuanzm/p/3995145.html

本文用到的参考连接如下:

1. MDN关于ol标签的介绍:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol

2. ol元素的相关属性:type、start、value和reversed: http://www.zhangxinxu.com/wordpress/2012/03/css-html-ol-type-start-value-reversed/

3. 伪元素 :before: https://developer.mozilla.org/en-US/docs/Web/CSS/::before

4. CSS counter: http://www.w3cplus.com/css3/css-counters.html