jquery使用jquery切换事件切换投票?

问题描述 投票:1回答:1

我有这个脚本工作正常,允许用户投票,但我有点如何我可以制作投票按钮,再次点击它切换回正常图像!就像stackoverflow一样:

这是我的脚本:

$(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();
        //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");
            }
        });
    });

HTML:

<ul class="statuses">
<li id="set_41" class="message">
 <span class="vote_count">0</span>
 <a href="#" class="vote_up"><img src="img/uparrow.png" /></a>

这是我想要发生的伪语言:

a user clicks vote image, it changes the image(uparrowActive)
//I've done that already
then
if user clicks the same image again it goes back to normal image(uparrow)
jquery html css toggle vote
1个回答
0
投票

jquery has a toggle event允许您将两个事件处理程序绑定到一个交替调用的元素。

*未经测试,可能有简单的语法错误。

myElement.toggle(vote(), unvote());

function vote()
{
   // add a vote to the item
   // change icon to vote icon
}

function unvote()
{
  // remove a vote from the item
  // change icon to default icon.
}
© www.soinside.com 2019 - 2024. All rights reserved.