JQuery Parent().下一期

Jas*_*son 2 asp.net jquery

这就是我所拥有的.我尝试了几件事,但我无法弄清楚我做错了什么......

<div class="VoteControls" runat="server" visible='<%# User.Identity.IsAuthenticated %>'>
    <img style="cursor: pointer; cursor: hand;" src='<%# (bool)Eval("skull") ? "images/skull.png" : "images/skull-bw.png" %>' alt="Vote Down" class="votedown" title='<%# Eval("entry.ID") %>' />
    <img style="cursor: pointer; cursor: hand;" src='<%# (bool)Eval("heart") ? "images/heart.png" : "images/heart-bw.png" %>' alt="Vote Up" class="voteup" title='<%# Eval("entry.ID") %>' />
</div>
Run Code Online (Sandbox Code Playgroud)

和JQuery:

      $(document).ready(function() {
            $(".voteup").click(function() {
                var id = $(this).attr("title");
                var userID = $("HiddenFieldUserID").val();
                var skullButton = $(this).parent().closest('.votedown');
                alert(skullButton.attr("src"));
                registerUpVote("up", id, $(this), skullButton, userID);
            });
            $(".votedown").click(function() {
                var id = $(this).attr("title");
                var userID = $("HiddenFieldUserID").val();
                var heartButton = $(this).parent().closest('.voteup');
                alert(heartButton.attr("src"));
                registerDownVote("down", id, heartButton, $(this), userID);
            });
        });
Run Code Online (Sandbox Code Playgroud)

目标是在单击.voteup img时,在同一个VoteControls div中找到相应的.votedown img.上面的div是DataList的一部分,因此页面上会有一堆这样的.

所以不起作用的部分是:

var skullButton = $(this).parent().closest('.votedown');
Run Code Online (Sandbox Code Playgroud)

SLa*_*aks 6

你误解了这个closest方法,它找到了元素最里面的元素.

你需要写$(this).siblings('.votedown').