jQuery选择器:选择具有特定高度的元素

bur*_*ang 3 jquery height element selector

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

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

Bru*_*uno 10

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

var $tds = $("td");
// better with a context 
// var context = "#mytable";
// var $tds = $("td", context);
Run Code Online (Sandbox Code Playgroud)

$ .fn.filter将匹配元素的集合减少为与选择器匹配的元素或传递函数的测试.

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

var $bigTds = $tds.filter(function () {
  return isHighterThan($(this), 200);
});
Run Code Online (Sandbox Code Playgroud)

最后将新样式应用于匹配元素.

$bigTds.each(function () {
  $(this).css("width", "400px");
});
Run Code Online (Sandbox Code Playgroud)


Laz*_*mov 9

<table>
  <tr>
    <td class="cell">
       ...
    </td>
  </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

jQuery的

$('.cell').each(function() {
   if($(this).height()>=200)
      $(this).width(...);
});
Run Code Online (Sandbox Code Playgroud)