asp.net
  • jquery
  • 2010-03-03 69 views 2 likes 
    2

    这就是我所拥有的。我已经试过几件事情,但我想不出什么我做错了......JQuery Parent()。下一期

    <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> 
    

    而且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); 
          }); 
         }); 
    

    我们的目标是在点击一个.voteup IMG时,在相同的VoteControls div中查找相应的.votedown img。上面的div是DataList的一部分,所以在页面上会有一堆。

    所以不工作的部分是:

    var skullButton = $(this).parent().closest('.votedown'); 
    
    +0

    你正在编写SO克隆吗? – SLaks 2010-03-03 22:26:22

    +0

    没有。只需要一个类似的投票系统,只有上/下(喜欢/不喜欢)。然而,从功能角度来说,SO是我最喜欢的网站,所以我可以应用于我所做的任何事情都是一场胜利。 = D – Jason 2010-03-03 23:01:52

    回答

    6

    你误解了closest方法,其中发现最里面父元素的。您需要编写$(this).siblings('.votedown')

    +0

    打败我吧。 +1! – 2010-03-03 22:27:15

    +0

    哦,我知道,我认为它说next()而不是最接近()....不应该接下来的工作?因为它不。 – Jason 2010-03-03 22:55:14

    +0

    兄弟姐妹()工作得很好。谢谢。 – Jason 2010-03-03 22:57:30

    相关问题