asp.net
  • jquery
  • 2010-03-03 57 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

    相關問題