1

ユーザーが投票できるようにするこのスクリプトは正常に機能しますが、投票ボタンを再度クリックすると通常の画像に切り替わる方法のようなものです! スタックオーバーフローと同じように:

これは私のスクリプトです:

$(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)
4

1 に答える 1

0

jquery には、交互に呼び出される 2 つのイベント ハンドラーを要素にバインドできるトグル イベントがあります。

*未テスト、おそらく単純な構文エラーがあります。

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.
}
于 2010-10-12T18:35:49.957 に答える