如何在jquery中获取最接近的img标签?

Vin*_*nay 3 javascript jquery javascript-events

HTML

<tr class='test_file'>
    <td id=img_id><img src='"+ROOT_PATH+"/assets/arrow_black_right.png' /></td>
    <td colspan=2 id=fileName>"+fileName+"</td>
</tr><span>
Run Code Online (Sandbox Code Playgroud)

我需要在jQuery中单击id = fileName的td时更改图像.

如何使用jQuery 获取img标记并更改其src属性?

我想做这样的事情:

$($(this).closest("img")).attr("src")
Run Code Online (Sandbox Code Playgroud)

Sus*_* -- 7

使用的组合.closest.find()

closest找到它的祖先

你需要找到它的后代而不是祖先

所以,你需要先找到最接近行,其中包含filenameID元素,然后findimg作为对应行的后裔

$(this).closest('tr').find("img").attr("src"); // using find
Run Code Online (Sandbox Code Playgroud)

要么

var $tr = $(this).closest('tr'); // get the closest row
$("img", $tr).attr("src");  // use a context to get the image
Run Code Online (Sandbox Code Playgroud)