2011-09-18 62 views
0

我需要将可变数量的表行组合到一个数组中。每个组都有在HTML如图5个单元:组表行到数组中

<tr> 
<td width='23%' class='table-item-1'><strong>EVP</strong></td> 
<td width='15%' class='table-item-1'>Diane</td> 
<td width='17%' class='table-item-1'>xxx-xxx-xxxx</td> 
<td width='17%' class='table-item-1'>xxx-xxx-6xxx</td> 
<td width='25%' class='table-item-1'>&nbsp;<a href='mailto:'></a></td> 
</tr> 
<tr> 
<td width='23%' class='table-item-1'><strong>EVP</strong></td> 
<td width='15%' class='table-item-1'>Leslee</td> 
<td width='17%' class='table-item-1'>xxx-xxx-xxxx</td> 
<td width='17%' class='table-item-1'>xxx-xxx-6xxx</td> 
<td width='25%' class='table-item-1'>&nbsp;<a href='mailto:'></a></td> 
</tr> 
<tr> 
<td width='23%' class='table-item-1'><strong>Production Mgr</strong></td> 
<td width='15%' class='table-item-1'><s>Lenny </s>&nbsp; tbh</td> 
<td width='17%' class='table-item-1'>xxx-xxx-xxxx</td> 
<td width='17%' class='table-item-1'>xxx-xxx-6xxx</td> 
<td width='25%' class='table-item-1'>&nbsp;<a href='mailto:'></a></td> 
</tr> 
<tr> 
<td width='23%' class='table-item-1'><strong>Production</strong></td> 
<td width='15%' class='table-item-1'>Jessica</td> 
<td width='17%' class='table-item-1'>xxx-xxx-xxxx</td> 
<td width='17%' class='table-item-1'>xxx-xxx-2xxx</td> 
<td width='25%' class='table-item-1'>&nbsp;<a href='mailto:'></a></td> 
</tr> 

这是我到目前为止有:

var items_size = $('[width|="23%"]').size(); //gives me the amount of groups 

var items1 = { 
     'name': $('.table-item-1 ~ td').html(), 
     'email': $('.table-item-1 ~ td > a').html(), 
     'phone': $('.table-item-1 ~ td').next().html(), 
     'fax': $('.table-item-1 ~ td').next().next().html(), 
     'title': $('.table-item-1 ~ td').prev().children().html() 
     }; // creates an array containing the items from the first group. 

我需要为每个组中的HTML同一阵列。

回答

1
var items = []; 
$('table tr').each(function() { 
    // access each tr's tds from here using $(this) 
    var item = { 
     'name' : $(this).find('td:first').text(), 
     ... 
    } 
    items.push(item); 
});