jquery根据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()
]
我正在尝试仅从 ID 为parent0 和child0 的 块捕获所有数据。
在当前工作状态下,该工具以文本形式捕获两个表中的所有数据。理想情况下,我希望能够单独捕获所有 块,将它们放入一个数组中,然后我可以对其进行迭代。
I'm a noob to jquery. Stuck and would appreciate some help.
Currently building a tool to grab data from a particular page. The page looks like this:
<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>
Below is a snippet of the 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()
]
I'm trying to capture all data from just the <tr>
blocks with id parent0, and child0.
In it's current working state, the tool captures all data within the two tables as text. Ideally I'd like to be able to capture all the <TR>
blocks separately, put them in an array that I can then iterate over.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您正在使用 ID 元素,而您应该使用类。
与
可以
ID 不同,您 使用 class 属性为多个元素分配相同的值。
然后你可以像这样选择它们
It looks like you are using ID elements where you should be using a class.
Instead of
Do this
Unlike ID, you can assign the same value to multiple elements with the class attribute.
Then you can select them all like this
这样你就会得到所有id为child0的tr
检查此链接以获取更多参考 http://api.jquery.com/category/selectors/
this way you will get all tr whose id is child0
check this link for more reference http://api.jquery.com/category/selectors/
这样做...
编辑:尽管这也将匹配“blablablabla”。
这样应该更准确。
编辑:实际上,我认为这个解决方案更优雅:
DO like that...
Edit: though that will match "blablablabla" as well.
That should be more accurate.
Edit: Actually, I think this solution is more elegant: