代码之家  ›  专栏  ›  技术社区  ›  StealthRT

jQuery通过TD循环并检查具有相同属性的值

  •  0
  • StealthRT  · 技术社区  · 8 年前

    td 属性

    var name = "";
    var theCell = "";
    var width1 = "";
    var width2 = "";
    var intX = 0;
    
    $("td").each(function (inx) {
         name = $(this).data("colname");
    
         theCell = $('[data-colname="' + name + '"]')[0];
         console.log(theCell);
    
         if (typeof theCell != 'undefined') {
              width1 = theCell.scrollWidth;   
              width2 = theCell.clientWidth;
    
              //console.log(width1);
    
              if (width1 > width2) {
                   //console.log(theCell);
                   $(theCell).ellipsis({ lines: 1 });
                   $(theCell).css({ 'background-color': '#000' });
              }
         }
    });
    

    的输出 安慰日志(单元格) :

    <td data-colname="number_0" class="jsgrid-cell jsgrid-align-left" style="width: 80px;">1</td>
    <td data-colname="line_0" class="jsgrid-cell jsgrid-align-left" style="width: 80px;">1</td>
    <td data-colname="network_0" class="jsgrid-cell jsgrid-align-left" style="width: 80px;">Welcome 1</td>
    ...more here...
    <td data-colname="number_0" class="jsgrid-cell jsgrid-align-left" style="width: 80px;">45</td>
    <td data-colname="line_0" class="jsgrid-cell jsgrid-align-left" style="width: 80px;">2</td>
    <td data-colname="network_0" class="jsgrid-cell jsgrid-align-left" style="width: 80px;">Welcome 2</td>
    ...more here...
    <td data-colname="number_0" class="jsgrid-cell jsgrid-align-left" style="width: 80px;">23</td>
    <td data-colname="line_0" class="jsgrid-cell jsgrid-align-left" style="width: 80px;">775</td>
    <td data-colname="network_0" class="jsgrid-cell jsgrid-align-left" style="width: 80px;">Welcome 3</td>
    ...more here...etc etc...
    

    页面上是这样的:

    enter image description here

    td 仅此而已。我想这是因为 但我不确定需要做什么才能让它循环通过 每个 td ?

    它是通过每个TD的循环,因为我把它都包装在 jquery每个 功能,但它只是在第一个循环 td data colname=“number\u 0” , data colname=“行0” 等)所以我不能用它来区分一排和另一排。。

    1 回复  |  直到 8 年前
        1
  •  1
  •   pete    8 年前

    我猜你想这么做:

    $('td').each(function (index, elem) {
        if (elem.scrollWidth > elem.clientWidth) {
            // console.log(elem);
            $(elem).ellipsis({ lines: 1 }).css({ 'background-color': '#000' });
        }
    });
    

    将第二个参数包含在 .each

    代码中的这一行:

    theCell = $('[data-colname="' + name + '"]')[0];
    

    只会撤回第一个 td [data-colname="' + name + '"] 选择器,因为表中的每一行似乎共享相同的内容 data-colname 属性