简体   繁体   English

表格的jQuery CSS选择器

[英]jQuery css selector for table

I'm having the following table markup: 我有以下表格标记:

<table>
    <tr><td>foo</td><td class=marked>foo</td><td>foo</td></tr>
    <tr><td class=marked>foo</td><td class=marked>foo</td><td class=marked>foo</td></tr>
    <tr><td>foo</td><td>foo</td><td>foo</td></tr>
    <tr><td>foo</td><td>foo</td><td class=marked>foo</td></tr>
</table>

Several cells are marked with class marked - how can I select all rows which have cells with class marked ? 一些细胞都标有类marked -我如何可以选择具有类细胞中的所有行marked I know how to select all marked cells but I need the rows for the selector in order to loop through them. 我知道如何选择所有标记的单元格,但我需要选择器的行才能遍历它们。

What I want to achieve is an array/JSON string which contains all data of all rows with cell indexes of the marked cells, which would look something like that: 我要实现的是一个数组/ JSON字符串,其中包含所有行的所有数据以及已标记单元格的单元格索引,看起来像这样:

rowdata[0] = [1];
rowdata[1] = [0,1,2];
rowdata[3] = [2];

Any ideas what's the easiest way to to it? 有什么想法是最简单的方法吗?

EDIT: Sorry, there was a typo, I meant class "marked" 编辑:对不起,有一个错字,我的意思是课堂"marked"

Try like 尝试像

var rowdata=[];

$('tr').each(function(){
   $td=$(this).find('td');
   arr=[];
   $td.each(function(index,value){
       if($(this).hasClass('marked'))
          arr.push(index);
   });
   rowdata.push(arr);
});

Fiddle 小提琴

var rowdata = [];
$('table tr').each(function () {
    rowdata.push($(this).find('.marked').map(function () {
        return $(this).index();
    }).get());
});

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM