创建一行 li,给最后一个类命名为“last”并将它们全部放在 ul 中
如何创建具有特定规格的 li 列表(通过使用 css 和向左浮动,它们看起来像简单的盒子):
- 连续 7 个 li,
- 该行是一个闭合的 ul 标签,
- 最后应该有 8 行
- ul最后一个里(第7个)应该有class =“last”。
这就是我到目前为止所得到的,通过数组创建 li(我需要一个数组,因为我需要这些值):
<script type="text/javascript">
$(function () {
$.each([39, 6, 1, 3, 5, 1, 1, 1, 1, 1, 1], function(index, value) {
$("#heatmap ul").append("<li class='box' value="+value+">"+index+"</li>");
});
});
</script>
我感谢您的帮助和时间, 妮可
how do I create a list of li's (they look like simple boxes by using css and float left) with the certain specifications:
- 7 li's in a row
- the row is a closed ul-tag
- at the end there should be 8 rows of ul's
- the last li (the 7th) should have the class="last".
this is what i got so far, to create li's through an array (i need an array, because i need the values):
<script type="text/javascript">
$(function () {
$.each([39, 6, 1, 3, 5, 1, 1, 1, 1, 1, 1], function(index, value) {
$("#heatmap ul").append("<li class='box' value="+value+">"+index+"</li>");
});
});
</script>
I appreciate your help and your time,
Nicole
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜是这样的吗?
数组中每 7 个项目输出一个
,并将
last
类添加到生成的Something like this I guess?
Outputs an
<ul>
per every 7 items in the array, and adds thelast
class to the last element in the generated<li/>
's我会缓存您的选择器并使用
:nth-child(7n)
选择器(每第 7 个子级),如下所示:
这可以防止它为函数中的每个循环找到
。
如果您浮动
元素,并给它们一个宽度,并且
为该宽度的 7 倍,它们将按 7 行浮动,如下所示:
最好展示浮动方法的演示: 这是我的意思的快速演示,这使用 1
而不是
n
元素,简单得多 :)
I'd cache your selector and use the
:nth-child(7n)
selector (every 7th child) afterwards, like this:This prevents it finding that
<ul>
for every loop in the function.If you float your
<li>
elements, and give them a width, and the<ul>
7x that width, they will float in rows of 7, like this:Best to show a demonstration of the floating approach: here's a quick demo of what I mean, this uses 1
<ul>
instead ofn
<ul>
elements, much simpler :)应该可以解决这个问题(因为 jQuery 可以解析 CSS3+ 选择器)
should do the trick (as jQuery can parse CSS3+ selectors)