你可以根据这种关系设计一个td吗?

时间:2013-08-13 21:00:59

标签: javascript jquery html css

说我喜欢这样......

<th>
  <div class="text-left field-sorting " rel="local_inventory"> Local inventory </div>
</th>
<th>
  <div class="text-left field-sorting " rel="something else1"> Something Else1 </div>
</th>
<th>
  <div class="text-left field-sorting " rel="something else2"> Something Else2 </div>
</th>

我希望关联td ...

<td>4</td>
<td>0</td>
<td></td>

在没有添加类的情况下拥有某种样式,是否可以根据if值是否存在(如果数字大于0且不为空)来设置此td的样式?我会用jQuery来应用CSS。否则我会破解自动化这个数据网格的PHP脚本。我知道有可能访问th,所以我想我的问题是如何使用它来访问相关的td,即使td除了在同一列之外没有任何独特的东西。

2 个答案:

答案 0 :(得分:1)

我想你想在这里使用.filter方法吗?

$('td').filter(function(){
  var text = $.trim($(this).text());
  return text.length && +text > 0;
}).addClass('greater-than-zero');

这是一个快速的小演示:http://jsbin.com/abovow/1/edit


我现在更好地理解这个问题了。怎么样?

$('td, th').addClass(function(idx){
 return 'col-' + ( $(this).index() + 1 );
});

使用演示:http://jsbin.com/abovow/4/edit

答案 1 :(得分:0)

您可以使用nth-child选择器定位td。这会为同一列中td's的所有相应th设置样式。

否则,我认为您不能使用th至少根据CSS条件编写选择器。

在这种情况下,使用jquery或添加类应该更适合您的需求。

tr td:nth-child(2) { // Targets the 2nd td inside each tr
    // Some style
}
相关问题