如何在TR的最后一个TD中添加事件或将ID属性添加到IMG标签

how add event or add id attribute to img tag in last td in tr

本文关键字:添加 ID 属性 标签 IMG 事件 TR 最后一个 TD      更新时间:2023-09-26

我需要将属性ID设置为以下突出显示的img标签找到突出显示的img标签并添加onclick事件。我怎样才能用jQuery做到这一点?


<img src="/site/control/uploader/phpuploader/resources/stop.png" ...

<tbody>
<tr class="AjaxUploaderQueueTableRow" style="background-color: rgb(255, 255, 255);">
<td style="white-space: nowrap;">
<img src="/site/control/uploader/phpuploader/resources/circle.png" title="" width="16" height="16">
</td>
<td style="width: 456px;">Tales&nbsp;from&nbsp;the&nbsp;Dark&nbsp;1&nbsp;(2013).jpg</td>
<td style="white-space: nowrap;">
<img src="/site/control/uploader/phpuploader/resources/stop.png" title="Remove" width="16" height="16" style="cursor: pointer;">
</td>
</tr>
</tbody>
我不知道

什么时候你想这样做,但这将使用jquery设置id:

$('tr.AjaxUploaderQueueTableRow img:last').attr('id', 'yourID');

要找到最后一个 td,然后找到 img 标签,因为 img 可能不是最后一个:

$('tr.AjaxUploaderQueueTableRow td:last img').attr('id', 'yourID');

如果你想点击它并添加一个"突出显示"类,那就是:

var lastTlbImg = $('tr.AjaxUploaderQueueTableRow td:last img');
lastTlbImg.click(function(){
    lastTlbImg.addClass('highlight');
})

更新

http://jsfiddle.net/kasperfish/2FCv6/2/

$("table:first tr td:last-child img").attr('id','blue').on('click', function(){
alert('clicked');
});