我正在尝试浏览已单击的表行,以从该行的其他部分提取数据。
这是单击的行的HTML代码段:
<tr><td class=" sorting_1">Foobar</td>
<td>Hello</td><td><a href="/some/path/abc.html">42</a></td>
<td>0</td>
<td><img src="/images/img.gif"></td>
<td>7:44 AM</td>
<td><ul><li><a href="/path2/read/3">Read it</a></li>
<li><a class="booboo" href="#">Just do it</a></li>
</ul></td>
单击的单元格元素具有“ booboo”类。
我希望能够选择以下数据:
前一个兄弟姐妹的网址中使用的ID(在上面的示例中为3)
第一列中的名称(在给出的示例中为“ Foobar”)
第二个单元格中的锚元素的网址(在此示例中,应为/some/path/abc.html)
谁能指出浏览表行所需的功能,最好是显示如何在上面显示的示例代码段中选择值的代码段?
最佳答案
$("table a.booboo").click(function() {
var $this = $(this);
var tr = $this.closest("tr");
// the id used in the url of the previous li a sibling
var a = $this.closest("ul").find("li:first a").attr("href");
a = a.substring(a.lastIndexOf("/") + 1);
alert(a);
// the name in the first column
var b = tr.find("td:first").text();
alert(b);
// the url of the anchor elem in the 2nd cell
var c = tr.find("td:eq(2) a").attr("href");
alert(c);
});
Demo on jsFiddle