要素の上位 2 つの親しか取得できず、それを超えないという厄介な問題があります。.closest() も機能しません。階層を見ると、ドキュメントに記載されている仕様に従って動作するはずです。
ここに私が持っているものがあります:
<div id="telephonezone" class="bbox">
<table id="phones" cellspacing="0" width="100%">
<thead>
<tr><th width="70">Type</th><th>Phone number</th><th width="30"></th>
</tr></thead>
<tbody><tr style="background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial;">
<td class="primaryset">N/A</td>
<td class="primaryset">0208 989 8183</td>
<td class="primaryset">
<img data-id="0" data-dbid="1126" src="images/dandy-color/cancel.png" title="Delete."></td>
</tr>
</tbody>
</table>
</div>
私のJquery:
$('.bbox tr td img').live('click', function(){
id = $(this).attr('data-dbid') ;
cont = $(this).parent().parent().html() ;
alert(cont);
})
cont = $(this).parent().parent().html() ; < tr > の内容を取得しますが、それよりも高くしようとすると、未定義になります。prevAll、prev、closerest、またはそれらの任意の組み合わせで同じ結果が得られます。
td 内の画像をクリックしたときに、1 行目の div の ID を選択するにはどうすればよいですか?
html += '><img data-id="'+i+'" data-dbid="'+v[4]+'" src="images/dandy-color/cancel.png" title="Delete." /></td>' ;