dougengqiu8031 2010-10-04 22:38 采纳率: 100%
浏览 41
已采纳

在jquery成功函数上更改图像?

i have this jquery voting script, everything is working fine, it just that the image at the success function of the ajax request is not changing the image?

jquery:

$("a.vote_down").click(function(){
    //get the id
    the_id = $(this).attr('id');



    //the main ajax request
        $.ajax({
            type: "POST",
            data: "action=vote_down&id="+$(this).attr("id"),
            url: "votes.php",
            success: function(msg)
            {
                $("span#votes_count"+the_id).fadeOut();
                $("span#votes_count"+the_id).html(msg);
                $("span#votes_count"+the_id).fadeIn();
                            // this is my problem here
                $("span#vote_buttons"+the_id).attr("src", "images/downvoteActive.png");

            }
        });
    });

html:

<span class='vote_buttons' id='vote_buttons<?php echo $row['id']; ?>'>
        <a href='javascript:;' class='vote_up' id='<?php echo $row['id']; ?>'>Vote Up!</a>

        <a href='javascript:;' class='vote_down' id='<?php echo $row['id']; ?>'>Vote Down!</a>

css:

a.vote_up, a.vote_down {
    display:inline-block;
    background-repeat:none;
    background-position:center;
    height:16px;
    width:16px;
    margin-left:4px;
    text-indent:-900%;
}

a.vote_up {
    background:url("images/upvote.png");
}

a.vote_down {
    background:url("images/downvote.png");
}

        </span>
  • 写回答

2条回答 默认 最新

  • dqvzfp6468 2010-10-04 22:41
    关注

    UPDATE:

    Further to the updated question, span elements don't have a src attribute.

    You also have another major issue. You are assigning the same id to more than one element: This is happening for the vote_up and vote_down links.

    From what I am understanding, you may want to simply assign a unique id to each vote_up and vote_down link, and them simply change their background url within the success callback.


    2nd UPDATE:

    Let's assign a unique id to each vote_up and vote_down link:

    <a ... class='vote_up' id='vote_up<?php echo $row['id']; ?>'> ...
    <a ... class='vote_down' id='vote_down<?php echo $row['id']; ?>'> ...
    

    Then you can try to replace the problematic attr() call with this:

    $("#vote_up" + the_id).css("background-image", "url(images/downvoteActive.png)");
    

    Previous Answer:

    You're trying to change the src attribute of a span element. I guess that should be an img, but you can leave out the tag name from the selector:

    $("#vote_buttons"+the_id).attr("src", "images/downvoteActive.png");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题