【问题标题】:jQuery .each function don't work properly on html tablejQuery .each 函数在 html 表上无法正常工作
【发布时间】:2017-09-26 01:24:41
【问题描述】:

我在 jQuery 中获取表格的尺寸时遇到问题。

我有一个这样的循环生成的表:

if(something) {
    echo '<tr class="trclass" height="'.$a['percentHeight'].'%" colspan="'.$a['col_span'].'">';
}

if(something else) {
    echo '<td class="tdclass" width="'.$a['percentWidth'].'%" rowspan="'.$a['row_span'].'">';
    if (condition == 1) echo $action['1'];
    if (condition == 2) echo $action['2'];
    if (condition == 3) echo $action['3'];
}
echo '</td>';

其中 col、row_span 和 width 和 height 和 action 是表单数据库。

  1. 这个表格在每一页上可能都不一样。

  2. 表格骨架的尺寸很好,动作显示在适当的单元格中,但我想使动作尺寸适合单元格尺寸。

但是当我尝试只在控制台中显示每个单元格的高度时,如下所示:

$('#table tr').each (function() {

    console.log($('td').height());

或者这个:

$('.tdclass').each (function() {

    console.log($(this).height());

它只显示一个数字,我无法更改每个单元格的 css。有人可以告诉我如何正确执行此操作吗?

【问题讨论】:

  • console.log($(this).height()); 我认为它显示一个数字,因为所有数字可能具有相同的高度,请检查控制台是否有重复值。
  • @bhansa 的回答似乎是正确的。还有一点,我不认为你可以为高度属性分配百分比值,最好使用 CSS (style="height: X%)..或者在 TD 中使用它而不是 TR ..COLSPAN 也适用于 TD 而不是 TR跨度>
  • 在控制台中,消息左侧的圆圈中是否有数字?

标签: php jquery html html-table


【解决方案1】:
$('#table tr').each (function() {
    console.log($($(this).find('td').get(0)).height());
});

$('#table tr').each (function() {
  $(this).find('td').each(function(){
    console.log($(this).height());
  })
});

【讨论】:

    【解决方案2】:

    试试下面的

    $("table").find("td").each(function() {
      console.log($(this).height());
    });
    

    你可以在这个Fiddle查看它

    【讨论】:

      猜你喜欢
      • 2016-05-25
      • 1970-01-01
      • 2020-08-26
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多