http://jsfiddle.net/twkRY/最接近()從之前的TD
<table>
<tr>
<td class="iwant">ttt</td>
<td>some text <span class="txt">haha</span></td>
</tr>
</table>
$(".txt").on("click", function() {
alert($(this).closest("td.iwant").html())
})
得到一個HTML值它只是給不確定的。爲什麼不ttt?
編輯:我知道解決如何使其與prev
和parent
工作,我只是想知道爲什麼closest
不起作用。
'jQuery.closest()'遍歷* *了,你想'jQuery.prev()'或'jQuery.prevAll( 'iwant')'。 –
'jQuery.closest()'更類似於'jQuery.parents()'。 –
'closest'不起作用,因爲'td.iwant'不是'.txt'元素的父親 –