Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何获得TD相对于表的索引,而不是TR_Jquery_Html_Html Table - Fatal编程技术网

Jquery 如何获得TD相对于表的索引,而不是TR

Jquery 如何获得TD相对于表的索引,而不是TR,jquery,html,html-table,Jquery,Html,Html Table,我在一个页面上有四个表,我想分别针对每个表中的表单元格索引,将表作为父表,而不是行。此代码返回每行中的索引 所以我想找到前三个TDs,每个表中都有一类“事件” $('.section .calendar').each(function(){ $(this).find('td.events').each(function(){ ind= $(this).index() ind <= 2 ? $(this).css('background','#f90') : $

我在一个页面上有四个表,我想分别针对每个表中的表单元格索引,将表作为父表,而不是行。此代码返回每行中的索引

所以我想找到前三个TDs,每个表中都有一类“事件”

$('.section .calendar').each(function(){  
 $(this).find('td.events').each(function(){
      ind= $(this).index()
      ind <= 2 ? $(this).css('background','#f90') : $(this).css('background','#000');

     })
  })
它将所有四个表中的单元格索引作为父表

ind= $(this).index()  
将每行(所有四个表)中的单元格索引作为其父级

表的结构通常是有效的:

<table class="calendar">
 <tr>
  <th></th>
  <th></th>
  <th></th>
  <th></th>
  <th></th>
  <th></th>
  <th></th>
</tr>
<tbody>
<tr>
 <td></td>
 <td class="events"></td>
 <td></td>
 <td class="events"></td>
 <td></td>
 <td></td>
 <td></td>
</tr>
<tr>
 <td></td>
 <td class="events"></td>
 <td></td>
 <td></td>
 <td></td>
 <td class="events"></td>
 <td></td>
</tr>

(……等)

  • 还有三张相同的桌子

您可以将所有tds集作为参数传递给索引方法:

$('.section .table').each(function(){  
 var childtds = $(this).find('td.events');
 childtds .each(function(){
  ind= $(this).index(childtds );
  ind <= 2 ? $(this).css('background','#f90') : $(this).css('background','#000');

  })
})
$('.section.table')。每个(函数(){
var childtds=$(this.find('td.events');
childtds.each(函数(){
ind=$(this).index(childtds);

你可以试试这样的东西。 它基本上是在当前TDs之前计算TDs的数量

function getTDIndex($td) {
    var index1 = $td.prevAll("td").length;
    var index2 = $td.closest("tr").prevAll("tr").find("td").length;

    var index = index1 + index2;
};

RaraituL的上述解决方案似乎有效:

$('.section').each(function(){
 $(this).find('td.events').each(function(){
  var index1 = $(this).prevAll("td").length;
  var index2 = $(this).closest("tr").prevAll("tr").find("td").length;
  var index = index1 + index2;
  index <= 6 ? $(this).css('background','#f90') : $(this).css('background','#000');
 })
})
$('.section')。每个(函数(){
$(this).find('td.events').each(function(){
var index1=$(this).prevAll(“td”).length;
var index2=$(此).tr.prevAll(“tr”).find(“td”).length;
var指数=index1+index2;

你试过“table.events”吗?
$('.section').each(function(){
 $(this).find('td.events').each(function(){
  var index1 = $(this).prevAll("td").length;
  var index2 = $(this).closest("tr").prevAll("tr").find("td").length;
  var index = index1 + index2;
  index <= 6 ? $(this).css('background','#f90') : $(this).css('background','#000');
 })
})