2010-10-12 70 views
0

即时尝试将此jQuery脚本转换为切换事件类型,他们是我的脚本,插图,以显示我想要发生的事情。我CNT似乎所以这里intrgate有云:jQuery的问题?

//myElement.toggle(vote(), unvote()); // this is the toggle event 

    $(document).ready(function() { 
      $('.statuses').delegate('.vote_up', 'click', function(e) { 

       //stop event 
       e.preventDefault(); 
       //get the id 
       var the_id = $(this).closest('.message').attr('id').split('_').pop(); 

//function vote() // this should be the vote up function 
       //the main ajax request 
       $.ajax({ 
        context: this, 
        type: "POST", 
         // Make sure "this" in the callback refers to the element clicked 

        data: "action=vote_up&id=" + the_id, 
        url: "ajax/votes.php", 
        success: function (msg) { 

         $(this).siblings("span.vote_count").html(msg).fadeIn(); 
          // get the child <img> and set its src 
         $(this).children("img").attr("src", "img/uparrowActive.png"); 
        } 
       }); 

//function unvote() thier should be another function here to unvote toggle 

$.ajax({ 
        context: this, 
        type: "POST", 
         // Make sure "this" in the callback refers to the element clicked 

        data: "action=vote_down&id=" + the_id, 
        url: "ajax/votes.php", 
        success: function (msg) { 

         $(this).siblings("span.vote_count").html(msg).fadeIn(); 
          // get the child <img> and set its src 
         $(this).children("img").attr("src", "img/uparrow.png"); 
        } 
       }); 
      }); 

我只是不知道如何将这种触发事件融入这个jQuery的片段,有人请帮助我我一直都努力了多年:))感谢

+0

你错过了一个结束括号。你的'代表'结束了什么? – Incognito 2010-10-12 20:21:21

+0

谢谢@user它不是关于语法现在,即时通讯只是想知道如何即将intergate这两个切换功能,我不是sem了解他们如何能适应,我即将如此新到这 – getaway 2010-10-12 20:26:44

回答

0
$('.statuses').delegate('.vote_up, .vote_down', 'click', function(e) { 
    var parent = $(this).closest('.statuses'), 
     vote = (($(this).hasClass("vote_up")) ? "up" : "down"); 

    if(parent.data("vote") == vote) { 
     return true; // If already voted up, do nothing, if already voted down, do nothing... 
    } 

    if(vote == "down") { // Vote down 
     src = "img/uparrow.png"; 
     action = "vote_down"; 
    } 
    else if(vote == "up") { // Vote up 
     src = "img/uparrowActive.png"; 
     action = "vote_up"; 
    } 

    // Setting current vote (up or down) 
    // So next time you toggle, we have the trace of the previous vote (up/down) 
    // And authorise only do the opposite 
    parent.data("vote", vote); 

      $.ajax({ 
       context: this, 
       type: "POST", 
        // Make sure "this" in the callback refers to the element clicked 

       data: "action=" + action + "&id=" + the_id, 
       url: "ajax/votes.php", 
       success: function (msg) { 

        $(this).siblings("span.vote_count").html(msg).fadeIn(); 
         // get the child <img> and set its src 
        $(this).children("img").attr("src", src); 
       } 
      }); 
}); 
+0

非常感谢你,但现在它甚至没有像以前一样工作..但是,谢谢,我会给你正确的答案,我只是要问另一个问题! – getaway 2010-10-12 21:49:14

+0

对不起,看起来我不明白你的需要(我的错;)) – 2010-10-12 21:57:13

1

做所以:

$('.statuses').delegate('.vote_up, .vote_down', 'click', function(e) { 
    if($(this).hasClass('.vote_up') { 
     src = "img/uparrowActive.png"; 
     action = "vote_up"; 
    } 
    else { 
     src = "img/uparrow.png"; 
     action = "vote_down"; 
    } 

      $.ajax({ 
       context: this, 
       type: "POST", 
        // Make sure "this" in the callback refers to the element clicked 

       data: "action=" + action + "&id=" + the_id, 
       url: "ajax/votes.php", 
       success: function (msg) { 

        $(this).siblings("span.vote_count").html(msg).fadeIn(); 
         // get the child <img> and set its src 
        $(this).children("img").attr("src", src); 
       } 
      }); 
}); 
+0

谢谢brillant答案,但我需要它切换,让jquery知道投票何时开启或关闭,所以它可以切换,就像stackoverflow一样! :))再次感谢 – getaway 2010-10-12 20:32:32

+0

通过测试 src属性很容易,如果src是“img/uparrow.png”,那么将src更改为img/uparrowActive.png并采取行动。 ;) – 2010-10-12 20:34:09

+0

哦好吧,所以它已经不再这 – getaway 2010-10-12 20:35:23