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>
我需要在jQuery中单击id = fileName的td时更改图像.
我想做这样的事情:
$($(this).closest("img")).attr("src")
解决方法
使用.closest和.find()的组合
最近的人发现它的祖先
你需要找到它的后代而不是祖先
所以你需要先找到最近的行,其中包含文件名id元素,然后找到img,它是相应行的后代
$(this).closest('tr').find("img").attr("src"); // using find
要么
var $tr = $(this).closest('tr'); // get the closest row $("img",$tr).attr("src"); // use a context to get the image