8

这是我不时遇到的一个问题,我通常尝试从后端的角度解决,但想知道其他人是否找到了一个神奇的解决方案来解决前端的这个问题。

给定一个 ul/li 列表,按字母顺序在标记中提供,来自 az:

<ul>
    <li>Alpha</li>
    <li>Bravo</li>
    <li>Charlie</li>
    <li>Delta</li>
    <li>Echo</li>
    <li>Foxtrot</li>
    <li>Golf</li>
    <li>Hotel</li>
    <li>India</li>
    <li>Juliet</li>
    <li>Kilo</li>
    <li>Lima</li>
    <li>Mike</li>
    <li>November</li>
    <li>Oscar</li>
    <li>Papa</li>
    <li>Quebec</li>
    <li>Romeo</li>
    <li>Sierra</li>
    <li>Tango</li>
    <li>Uniform</li>
    <li>Victor</li>
    <li>Whiskey</li>
    <li>X-ray</li>
    <li>Yankee</li>
    <li>Zulu</li>
</ul>

通常,将项目向左浮动并在视觉上以块的形式对它们进行水平排序非常容易,如下所示:

一个 UL/LI 列表,其中 li 元素向左浮动

但是,要获取列,如下所示:

四个 UL/LI 列表,其中 ul 元素向左浮动

我总是不得不将 HTML 分解为单独的实体,例如<ul>上面示例中的四个单独的元素。

有没有一种方法可以将它全部保存在一个 ul 列表中而无需任何额外的标记,只使用 CSS(无 JavaScript)来获得像上面第二张图片那样的柱状外观?我的猜测是“不”,但我以前见过一些魔法,我想更明确地回答这个问题。

4

4 回答 4

4

恐怕还没有。

CSS3 有一些不错的功能,例如参见http://www.quirksmode.org/css/multicolumn.html但 Internet Explorer 不支持它,我不知道 IE9 中的支持情况如何(根据Microsoft非目前存在于测试版中)

于 2011-01-04T17:22:49.880 回答
1

If you're doing it in PHP you can use a simple counter and %. I've managed to accomplish this in WordPress getting a list of custom taxonomies like so:

<div class="row gutters">

    <?php $taxos = get_categories ( array( 'taxonomy' => 'make' ) ); ?>

    <?php $count = 0; ?>

    <div class="col col_2">

        <ul>

            <?php foreach( $taxos as $tax ) : ?>

                <li><a href="/make/<?php echo $tax->slug; ?>"><?php echo $tax->name; ?></a></li>

                <?php $count++; ?>

                <?php if( $count % 5 == 0 ) echo '</div></ul><div class="col col_2"><ul>'; ?>

            <?php endforeach; ?>

        </ul>

    </div>

</div>

You can also loop through an array and acheive the same thing. Output looks something like:

a    f
b    g
c    h
d    i
e    j
于 2015-02-13T04:05:04.463 回答
1

您可以使用 CSS3 多列布局及其 polyfill:https ://github.com/gryzzly/CSS-Multi-column-Layout-Module-Polyfill

于 2012-02-21T03:06:26.480 回答
1

我今天需要它并写了这个 python 等价物。您可以使用本机 javascript 编写。

# -*- coding: utf8 -*-
import math
# --------------------- EDIT THIS PART
#define columns
c = 4
# you can define to alphabetical list here
# i used to numbers, because range func. very effective for testing
list = range(1, 26, 1)
# ---------------------- END OF EDIT
# firstly sort

list = sorted(list)

# calculate total row
r = int(math.ceil(len(list) / c)) + (1 if len(list) % c is not 0 else 0)

index = 0
table1 = []
for x in range(c):
    table1.append([])
    for y in range(r):
        if len(list) > index:
            table1[x].append(y)
            table1[x][y] = list[index]
            index += 1


res = ''
for i in range(r):
    for x in table1:
        try:
            #print x[i]
            res += "%s\t|\t" % x[i]
        except IndexError:
            res += "%s\t|\t" % "_"
            pass
    res += "\n"

#print table1
print res

https://github.com/berkantaydin/vertical-alphabetical-sorting-with-columns

于 2014-02-24T12:35:38.153 回答