创建一行 li,给最后一个类命名为“last”并将它们全部放在 ul 中

发布于 2024-08-28 04:56:32 字数 528 浏览 4 评论 0原文

如何创建具有特定规格的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慢慢从新开始 2024-09-04 04:56:32

我猜是这样的吗?

var values = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];

var $heatmap = $('#heatmap');

var currentRow = "";
for(var i = 0; i < values.length; i++) {
    currentRow += "<li>" + values[i] + "</li>";

    if(i % 7 == 0) {
        $heatmap.append("<ul>" + currentRow + "</ul>");
        currentRow = "";
    }
}

if(currentRow !== "") {
    $heatmap.append("<ul>" + currentRow + "</ul>");
}

$heatmap.find("ul li:last").addClass("last");

数组中每 7 个项目输出一个

    ,并将 last 类添加到生成的

  • < 中的最后一个元素/代码>的

Something like this I guess?

var values = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];

var $heatmap = $('#heatmap');

var currentRow = "";
for(var i = 0; i < values.length; i++) {
    currentRow += "<li>" + values[i] + "</li>";

    if(i % 7 == 0) {
        $heatmap.append("<ul>" + currentRow + "</ul>");
        currentRow = "";
    }
}

if(currentRow !== "") {
    $heatmap.append("<ul>" + currentRow + "</ul>");
}

$heatmap.find("ul li:last").addClass("last");

Outputs an <ul> per every 7 items in the array, and adds the last class to the last element in the generated <li/>'s

时光病人 2024-09-04 04:56:32

我会缓存您的选择器并使用 :nth-child(7n)选择器(每第 7 个子级),如下所示:

$(function () {
  var ul = $("#heatmap ul");
  $.each([39, 6, 1, 3, 5, 1, 1, 1, 1, 1, 1], function(index, value) { 
    ul.append("<li class='box' value="+value+">"+index+"</li>"); 
  });
  ul.children(":nth-child(7n)").addClass("last");
});

这可以防止它为函数中的每个循环找到

如果您浮动

  • 元素,并给它们一个宽度,并且
      为该宽度的 7 倍,它们将按 7 行浮动,如下所示:
  • li { float: left; width: 100px; }
    ul { width: 700px }
    

    最好展示浮动方法的演示: 这是我的意思的快速演示,这使用 1

    I'd cache your selector and use the :nth-child(7n) selector (every 7th child) afterwards, like this:

    $(function () {
      var ul = $("#heatmap ul");
      $.each([39, 6, 1, 3, 5, 1, 1, 1, 1, 1, 1], function(index, value) { 
        ul.append("<li class='box' value="+value+">"+index+"</li>"); 
      });
      ul.children(":nth-child(7n)").addClass("last");
    });
    

    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:

    li { float: left; width: 100px; }
    ul { width: 700px }
    

    Best to show a demonstration of the floating approach: here's a quick demo of what I mean, this uses 1 <ul> instead of n <ul> elements, much simpler :)

    梦毁影碎の 2024-09-04 04:56:32
    $("#heatmap ul li:last").addClass('last');
    

    应该可以解决这个问题(因为 jQuery 可以解析 CSS3+ 选择器)

    $("#heatmap ul li:last").addClass('last');
    

    should do the trick (as jQuery can parse CSS3+ selectors)

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文