简体   繁体   中英

How to select the previous element in jquery

I have this html mark up:

<td>
    <label for="fadsf"> fadsf</label>
</td>
<td>
    <a class="editLink" href="/Admin/MenuManagement?Length=5">Edit</a>
</td>

What i want to do is to select the text in the label. But I find it difficult to do.

I tried this:

    $(".editLink").click(function () {
    alert(
     $(".editLink").prev("td").html()
     );
});

But it returns null.

$(".editLink").click(function () {
             $(".editLink").parent().prev().find("label").html()
        });

prev() and next() are used for siblings. You need to find the previous <td> , then the label inside it:

$(".editLink").closest('td').prev('td').find('label')

You are looking for parent() Not prev() selecting the previous sibling where there is none in your case.

See api.jquery.com/prev/

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