1
好的,所以我需要選擇一個TD並讓它改變裏面的圖像,這很好。但是,我需要能夠同時顯示所選td的ID,但是我的功能必須選擇圖像才能更改它。有沒有辦法做到這一點,所以它會選擇圖像,改變它,然後顯示帶有td id的警報?下面的代碼jQuery選擇問題
的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>
是啊,我的問題是,採用這種 '警報($(本).attr( 「TD ID」));' 或本 '警報($(本).attr( 「ID」) );' 選擇img標籤,向我提供'undefined'作爲輸出。 這就是我正在尋找的答案,謝謝! – 2013-02-27 20:56:07