【问题标题】:jQuery Selector: Select Element with Specific HeightjQuery 选择器:选择具有特定高度的元素
【发布时间】:2012-12-05 20:16:29
【问题描述】:

我有一张桌子。有些单元格有很多文本,有些单元格只有一个字符。

我想更改所有高度大于或等于 200 像素的单元格的宽度

【问题讨论】:

  • 嗨,你尝试过什么?
  • @Sharlike 当我的表格中的单元格有很多文本时,它会加高行。我希望它改为扩大列。

标签: jquery height element selector


【解决方案1】:

让我们从选择单元格开始:

var $tds = $("td");
// better with a context 
// var context = "#mytable";
// var $tds = $("td", context);

$.fn.filter 将匹配元素的集合减少为匹配选择器或通过函数测试的元素。

function isHighterThan ($el, threshold) {
  return $el.height() >= threshold;
}

var $bigTds = $tds.filter(function () {
  return isHighterThan($(this), 200);
});

最后为匹配的元素应用新样式。

$bigTds.each(function () {
  $(this).css("width", "400px");
});

【讨论】:

    【解决方案2】:
    <table>
      <tr>
        <td class="cell">
           ...
        </td>
      </tr>
    </table>
    

    jQuery

    $('.cell').each(function() {
       if($(this).height()>=200)
          $(this).width(...);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      • 2017-09-13
      • 2011-10-13
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 2012-12-01
      相关资源
      最近更新 更多