gpt4 book ai didi

jQuery 选择问题

转载 作者:行者123 更新时间:2023-11-28 13:22:47 24 4
gpt4 key购买 nike

好的,所以我需要选择一个 td 并让它更改其中的图像,效果很好。但是,我需要能够同时显示所选 td 的 id,但我的功能必须选择图像才能更改它。有没有办法同时执行这两种操作,以便它选择图像,更改图像,然后显示其中带有 td id 的警报?代码如下

j查询:

$(document).ready(function(){
$("td img").click(function() {
$(this).attr('src', 'images/d.gif');
alert($(this).attr("td id"));
return false;
});
});

html

<div id='plan'>
<table>
<tr>
<td class='n' id='a1'><img src='images/a.gif'/></td>
<td class='n' id='a2'><img src='images/a.gif'/></td>
<td class='n' id='a3'><img src='images/a.gif'/></td>
<td class='n' id='a4'><img src='images/a.gif'/></td>
<td></td>
<td class='n' id='a6'><img src='images/a.gif'/></td>
<td class='n' id='a7'><img src='images/a.gif'/></td>
<td class='n' id='a8'><img src='images/a.gif'/></td>
<td class='n' id='a9'><img src='images/a.gif'/></td>
</tr>
<tr>
<td class='n' id='b1'><img src='images/a.gif'/></td>
<td class='n' id='b2'><img src='images/a.gif'/></td>
<td class='n' id='b3'><img src='images/1.gif'/></td>
<td class='n' id='b4'><img src='images/1.gif'/></td>
<td></td>
<td class='n' id='b6'><img src='images/1.gif'/></td>
<td class='n' id='b7'><img src='images/a.gif'/></td>
<td class='n' id='b8'><img src='images/a.gif'/></td>
<td class='n' id='b9'><img src='images/a.gif'/></td>
</tr>
<tr>
<td class='n' id='c1'><img src='images/1.gif'/></td>
<td class='n' id='c2'><img src='images/1.gif'/></td>
<td class='n' id='c3'><img src='images/a.gif'/></td>
<td class='n' id='c4'><img src='images/a.gif'/></td>
<td></td>
<td class='n' id='c6'><img src='images/1.gif'/></td>
<td class='n' id='c7'><img src='images/1.gif'/></td>
<td class='n' id='C8'><img src='images/a.gif'/></td>
<td class='n' id='C9'><img src='images/a.gif'/></td>
</tr>
<tr>
<td class='n' id='d1'><img src='images/1.gif'/></td>
<td class='n' id='d2'><img src='images/1.gif'/></td>
<td class='n' id='d3'><img src='images/1.gif'/></td>
<td class='n' id='d4'><img src='images/1.gif'/></td>
<td></td>
<td class='n' id='d6'><img src='images/1.gif'/></td>
<td class='n' id='d7'><img src='images/1.gif'/></td>
<td class='n' id='d8'><img src='images/1.gif'/></td>
<td class='n' id='d9'><img src='images/1.gif'/></td>
</tr>
<tr>
<td class='p' id='e1'><img src='images/1.gif'/></td>
<td class='p' id='e2'><img src='images/1.gif'/></td>
<td class='p' id='e3'><img src='images/1.gif'/></td>
<td class='p' id='e4'><img src='images/1.gif'/></td>
<td></td>
<td class='p' id='e6'><img src='images/a.gif'/></td>
<td class='p' id='e7'><img src='images/a.gif'/></td>
<td class='p' id='e8'><img src='images/1.gif'/></td>
<td class='p' id='e9'><img src='images/1.gif'/></td>
</tr>
</table>
</div>

最佳答案

我想你的意思是 $(this).parent().attr("id");

您的代码:

alert($(this).attr("td id"));

实际上是在 img 标签上寻找名为 td id 的属性。

关于jQuery 选择问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15122137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com