我有桌子,桌子的每个单元格都有特定的网址.我试图使用jquery获取特定单元格上的URL.这里的问题是没有为表,行和列定义类或id.只想使用标签来拉取href属性.
<table>
<tr>
<td><a href='MytestSite11.com'>site21</a></td>
<td><a href='MytestSite12.com'>site22</a></td>
<td><a href='MytestSite13.com'>site23</a></td>
</tr>
<tr>
<td><a href='MytestSite21.com'>site21</a></td>
<td><a href='MytestSite22.com'>site22</a></td>
<td><a href='MytestSite23.com'>site23</a></td>
</tr>
<tr>
<td><a href='MytestSite31.com'>site21</a></td>
<td><a href='MytestSite32.com'>site22</a></td>
<td><a href='MytestSite33.com'>site23</a></td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
我想获得第二行第二列的href元素,即MytestSite22.我试过跟随jquery代码,但它正在返回我的undefine.Not确定错过了什么.
$(function () {
var $row = $('table tr').eq(1);
var $col=$row.eq(1)
alert($col.attr('href'));
});
Run Code Online (Sandbox Code Playgroud)
您忘了在第二个变量中指定元素:
var $col=$row.find('td').eq(1)
Run Code Online (Sandbox Code Playgroud)
然后,在其中指定锚点:
alert($col.find('a').attr('href'));
Run Code Online (Sandbox Code Playgroud)
对于它的价值,美元字符作为变量前缀通常用于表示数组或集合.在这里,您在单个项目上使用它.