1

好的,所以我需要选择一个 td 并让它更改其中的图像,这很好。但是,我需要能够同时显示选择的 td 的 id,但是我的函数必须选择图像来更改它。有没有办法同时做到这一点,以便 onclich 它选择图像,更改它,然后显示带有 td id 的警报?下面的代码

jQuery:

$(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>
4

1 回答 1

2

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

你的代码:

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

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

于 2013-02-27T20:52:48.943 回答