我是jquery的noob。卡住,希望得到一些幫助。 目前正在構建一個工具來抓取特定頁面的數據。該網頁看起來是這樣的:基於id的jquery匹配元素
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1">
<tbody> //This is data group 1
<tr id="parent0"> //Record 1
<td align="left"> <---------- text1 here -------> </td>
<td align="left"> <---------- text2 here -------> </td>
<td align="left"> <---------- text3 here -------> </td>
</tr>
<tr id="parent0"> //Record 2
<td align="left"> <---------- text1 here -------> </td>
<td align="left"> <---------- text2 here -------> </td>
<td align="left"> <---------- text3 here -------> </td>
</tr>
</tbody>
</table>
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1">
<tbody> //This is data group 2
<tr id="child0"> //Record 1
<td align="left"> <---------- text1 here -------> </td>
<td align="left"> <---------- text2 here -------> </td>
<td align="left"> <---------- text3 here -------> </td>
</tr>
<tr id="child0"> //Record 2
<td align="left"> <---------- text1 here -------> </td>
<td align="left"> <---------- text2 here -------> </td>
<td align="left"> <---------- text3 here -------> </td>
</tr>
</tbody>
</table>
下面是jQuery的一個片段:
ancestor = $(this).closest("tr[id]");
matchedElement = $(this).first();
originalBgColor = $(matchedElement).css('background-color');
$(matchedElement).css('background-color', 'green');
$(matchedElement).bind('click.annotator', function(event) {
event.stopPropagation();
event.preventDefault();
self.port.emit('show',
[
document.location.toString(),
$(ancestor).attr("child0"),
$(matchedElement).text()
]
我試圖從剛剛<tr>
塊id爲parent0和child0捕獲所有數據。
在當前的工作狀態下,該工具將文本捕捉到兩個表格中的所有數據。理想情況下,我希望能夠分別捕獲所有<TR>
塊,並將它們放入一個數組中,然後我可以迭代。
的ID值必須是唯一的,而你目前分配同一個ID多個元素。 – Paul 2012-01-11 00:57:40
請勿多次使用任何ID:http://www.w3.org/TR/html4/struct/global.html#h-7.5.2 – 2012-01-11 00:57:44