Javascript 使用jQuery在相邻的表单元格中触发链接
我有一个HTML表,其中每行的第一个单元格包含文本,第二个单元格包含链接。像这样:Javascript 使用jQuery在相邻的表单元格中触发链接,javascript,jquery,html,dom,javascript-events,Javascript,Jquery,Html,Dom,Javascript Events,我有一个HTML表,其中每行的第一个单元格包含文本,第二个单元格包含链接。像这样: <table id="foo"> <tr> <td>Some text</td> <td><a href="/path/to/file/"><img src="/path/to/image.jpg" /></a></td> </tr> ... </table&g
<table id="foo">
<tr>
<td>Some text</td>
<td><a href="/path/to/file/"><img src="/path/to/image.jpg" /></a></td>
</tr>
...
</table>
这不管用。我尝试过几种不同的方法,但没有成功。我意识到我可以将文本和链接放在同一个单元格中,最终可能会这样做,但这只是我的页面实际情况的简化版本,需要做一些工作来重新调整内容
我做错了什么?以编程方式单击链接不会转到
href
,只有本地(用户)单击才会,但您可以模拟它,如下所示:
$('#foo tr td:first-child').hover(...).click(
function() {
$(this).next().children().click();
}
);
$('#foo tr td:first-child').hover(...).click(
function() {
window.location.href = $(this).next().children('a').attr('href');
}
);
这会将设置为转到目标URL。以编程方式单击链接不会转到
href
,只有本地(用户)单击才会,但您可以模拟它,如下所示:
$('#foo tr td:first-child').hover(...).click(
function() {
$(this).next().children().click();
}
);
$('#foo tr td:first-child').hover(...).click(
function() {
window.location.href = $(this).next().children('a').attr('href');
}
);
这会将设置为转到目标URL