简体   繁体   中英

closest() for getting a html value from the previous td

http://jsfiddle.net/twkRY/

<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())
})

It just gives undefined. Why not ttt ?

edit : i know the solution how to make it work with prev and parent , I just want to know why closest doesn't work.

您想要获得closest td ,然后使用prev获得先前的兄弟姐妹。

$(this).closest("td").prev(".iwant").html()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM