为页面上的每个表运行Jquery Function

茉莉花

我有多个表是从wp中的循环生成的,并编写了jquery来删除具有空td的列,该表适用于第一个表,然后在所有其他表上添加相同的列。我试过将“每个”与jquery一起为每个表运行,但事实并非如此。

例:

每个表都有1行,并且列数相同(7)。空列被隐藏,这适用于单个表。可以说表1有2个可见列,表2有3个可见列。第3列(来自表2)被添加到表1中,即使它为空。

这是我的jQuery

    $(document).ready(function() {

    $('.man-table').each(function (table) {
        //count # of columns
        var numCols = $("th", table).length;
        for ( var i=1; i<=numCols; i++ ) {
            var empty = true;
            //grab all the <td>'s of the column at i
            $("td:nth-child(" + i + ")", table).each(function(index, el) {
                //check if the <span> of this <td> is empty
                if ( $("span", el).text() != "" ) {
                    empty = false;
                    return false; //break out of each() early
                }
            });
            if ( empty ) {
                $("td:nth-child(" + i + ")", table).hide(); //hide <td>'s
                $("th:nth-child(" + i + ")", table).hide(); //hide header <th>
            }
        }
    })
});

这是我为每个循环生成的表

        <table class='man-table'>
        <thead>
        <tr>
            <th class='numeric'><span>inductance (r)</span></th>
            <th class='numeric'><span>mount</span></th>
            <th class='numeric'><span>dim (mm)</span></th>
            <th class='numeric'><span>r current</span></th>
            <th class='numeric'><span>impedance</span></th>
            <th class='numeric'><span>capacitance</span></th>
            <th class='numeric'><span>resistance</span></th>
            <th class='numeric'><span>spec</span></th>
        </tr>
        </thead>
        <tr>
            <td data-title='inductance (r)' class='numeric'><span>{$ind}</span></td>
            <td data-title='mount type' class='numeric'><span>{$mnt}</span></td>
            <td data-title='dimensions' class='numeric'><span>{$dim}</span></td>
            <td data-title='rated current' class='numeric'><span>{$rat}</span></td>
            <td data-title='impedance' class='numeric'><span>{$imp}</span></td>
            <td data-title='capacitance' class='numeric'><span>{$cap}</span></td>
            <td data-title='resistance' class='numeric'><span>{$res}</span></td>
            <td data-title='spec sheet' class='numeric'><span><a href='{$site_url}/product-spec/{$prod_pdf}' target='_blank;'><div id='spec-btn'>DOWNLOAD</div></a></span></td>
        </tr>
    </table>
曲折的

这是一个基本示例,可以处理许多表并标识“空”列。假设表只有1行。

$(function() {

  function findEmptyCol(t) {
    var cols = [];
    $("tbody td", t).each(function(i, c) {
      if ($(c).text().trim() == "") {
        cols.push($(c).index());
      }
    });
    return cols;
  }

  function hideEmptyCol(tables) {
    tables.each(function(i, t) {
      var e = findEmptyCol(t);
      if (e.length) {
        $.each(e, function(k, v) {
          $("th", t).eq(v).hide();
          $("td", t).eq(v).hide();
        });
      }
    });
  }

  hideEmptyCol($(".man-table"));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3>Table 1</h3>
<table class='man-table' id="table-1">
  <thead>
    <tr>
      <th class='numeric'><span>Col 1</span></th>
      <th class='numeric'><span>Col 2</span></th>
      <th class='numeric'><span>Col 3</span></th>
      <th class='numeric'><span>Col 4</span></th>
      <th class='numeric'><span>Col 5</span></th>
      <th class='numeric'><span>Col 6</span></th>
      <th class='numeric'><span>Col 7</span></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class='numeric'><span>1</span></td>
      <td class='numeric'><span>2</span></td>
      <td class='numeric'><span>3</span></td>
      <td class='numeric'><span>4</span></td>
      <td class='numeric'><span>5</span></td>
      <td class='numeric'><span>6</span></td>
      <td class='numeric'><span>7</span></td>
    </tr>
  </tbody>
</table>
<h3>Table 2</h3>
<table class='man-table' id="table-2">
  <thead>
    <tr>
      <th class='numeric'><span>Col 1</span></th>
      <th class='numeric'><span>Col 2</span></th>
      <th class='numeric'><span>Col 3</span></th>
      <th class='numeric'><span>Col 4</span></th>
      <th class='numeric'><span>Col 5</span></th>
      <th class='numeric'><span>Col 6</span></th>
      <th class='numeric'><span>Col 7</span></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class='numeric'><span>1</span></td>
      <td class='numeric'><span></span></td>
      <td class='numeric'><span>3</span></td>
      <td class='numeric'><span>4</span></td>
      <td class='numeric'><span> </span></td>
      <td class='numeric'><span>6</span></td>
      <td class='numeric'><span>7</span></td>
    </tr>
  </tbody>
</table>

<h3>Table 3</h3>
<table class='man-table' id="table-3">
  <thead>
    <tr>
      <th class='numeric'><span>Col 1</span></th>
      <th class='numeric'><span>Col 2</span></th>
      <th class='numeric'><span>Col 3</span></th>
      <th class='numeric'><span>Col 4</span></th>
      <th class='numeric'><span>Col 5</span></th>
      <th class='numeric'><span>Col 6</span></th>
      <th class='numeric'><span>Col 7</span></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class='numeric'><span>1</span></td>
      <td class='numeric'><span>2</span></td>
      <td class='numeric'><span>3</span></td>
      <td class='numeric'><span>4</span></td>
      <td class='numeric'><span>5</span></td>
      <td class='numeric'><span>&nbsp;</span></td>
      <td class='numeric'><span>7</span></td>
    </tr>
  </tbody>
</table>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在每个页面上运行函数的最佳方式

来自分类Dev

即使在添加 $(document).ready(function () 后,javascript 仍可在控制台中运行但不在页面上运行

来自分类Dev

使用jQuery change事件过滤表,但可处理页面上的每个表

来自分类Dev

AlertDialog 函数在 flutter 中的每个页面上运行

来自分类Dev

我应该在页面上的每个组件中还是在父页面上为每个组件多次调用Axios?

来自分类Dev

jQuery脚本未在Django分页页面上运行

来自分类Dev

在通过ajax加载的页面上运行jQuery代码

来自分类Dev

(function($){})(jQuery)和jQuery(function(){})之间的区别;

来自分类Dev

SSRS在每个页面上重复页面标题

来自分类Dev

我如何添加一段HTML代码以在每个WordPress页面上运行

来自分类Dev

在页面上的每个 img 元素之后运行一个脚本块

来自分类Dev

jQuery中的function(event)vs function()

来自分类Dev

使用nth-child / type自动为页面上的每个链接设置不同的样式?

来自分类Dev

HTML和Javascript在页面上为目录中的每个图像创建新图像

来自分类Dev

HTML和Javascript在页面上为目录中的每个图像创建新图像

来自分类Dev

使用nth-child / type自动为页面上的每个链接设置不同的样式?

来自分类Dev

为页面上的每个 mySQL 条目添加一个独特的样式类?

来自分类Dev

尝试为 HTML 页面上显示的每个 MySQL 行制作“喜欢”按钮

来自分类Dev

在页面上添加所有 Facebook 广告,而无需为每个广告创建 Zap

来自分类Dev

输出到[Word]文档时,如何在SSRS中的每个页面上重复表组标题?

来自分类Dev

ViewModel在页面上为Null

来自分类Dev

ViewModel在页面上为Null

来自分类Dev

Recurservly initialize the jQuery function

来自分类Dev

jQuery(...).yiiGridView is not a function

来自分类Dev

jQuery .one(“ click”,function())

来自分类Dev

什么$(function(){...}); 在jQuery中?

来自分类Dev

如何运行python命令,该命令将单击页面上的每个链接并提取每个链接的标题,内容和日期?

来自分类Dev

引导表在页面上居中

来自分类Dev

Oracle Function更新表,如果记录为空则INSERT

Related 相关文章

  1. 1

    在每个页面上运行函数的最佳方式

  2. 2

    即使在添加 $(document).ready(function () 后,javascript 仍可在控制台中运行但不在页面上运行

  3. 3

    使用jQuery change事件过滤表,但可处理页面上的每个表

  4. 4

    AlertDialog 函数在 flutter 中的每个页面上运行

  5. 5

    我应该在页面上的每个组件中还是在父页面上为每个组件多次调用Axios?

  6. 6

    jQuery脚本未在Django分页页面上运行

  7. 7

    在通过ajax加载的页面上运行jQuery代码

  8. 8

    (function($){})(jQuery)和jQuery(function(){})之间的区别;

  9. 9

    SSRS在每个页面上重复页面标题

  10. 10

    我如何添加一段HTML代码以在每个WordPress页面上运行

  11. 11

    在页面上的每个 img 元素之后运行一个脚本块

  12. 12

    jQuery中的function(event)vs function()

  13. 13

    使用nth-child / type自动为页面上的每个链接设置不同的样式?

  14. 14

    HTML和Javascript在页面上为目录中的每个图像创建新图像

  15. 15

    HTML和Javascript在页面上为目录中的每个图像创建新图像

  16. 16

    使用nth-child / type自动为页面上的每个链接设置不同的样式?

  17. 17

    为页面上的每个 mySQL 条目添加一个独特的样式类?

  18. 18

    尝试为 HTML 页面上显示的每个 MySQL 行制作“喜欢”按钮

  19. 19

    在页面上添加所有 Facebook 广告,而无需为每个广告创建 Zap

  20. 20

    输出到[Word]文档时,如何在SSRS中的每个页面上重复表组标题?

  21. 21

    ViewModel在页面上为Null

  22. 22

    ViewModel在页面上为Null

  23. 23

    Recurservly initialize the jQuery function

  24. 24

    jQuery(...).yiiGridView is not a function

  25. 25

    jQuery .one(“ click”,function())

  26. 26

    什么$(function(){...}); 在jQuery中?

  27. 27

    如何运行python命令,该命令将单击页面上的每个链接并提取每个链接的标题,内容和日期?

  28. 28

    引导表在页面上居中

  29. 29

    Oracle Function更新表,如果记录为空则INSERT

热门标签

归档