jQuery选择问题

时间:2013-02-27 20:51:16

标签: jquery html css jquery-selectors html-table

好的,所以我需要选择一个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>

1 个答案:

答案 0 :(得分:2)

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

您的代码:

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

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