【问题标题】:changing the image on jquery success function?更改 jquery 成功功能上的图像?
【发布时间】:2010-10-05 07:18:50
【问题描述】:

我有这个 jquery 投票脚本,一切正常,只是 ajax 请求成功函数处的图像没有改变图像?

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>

【问题讨论】:

  • 我认为重写整个 html 和 css 对我来说更好,因为它不能很好地补充 jquery

标签: php jquery html css ajax


【解决方案1】:

更新:

关于更新后的问题,span 元素没有 src 属性。

您还有另一个重大问题。您将相同的 id 分配给多个元素:vote_upvote_down 链接会发生这种情况。

据我了解,您可能只想为每个vote_upvote_down 链接分配一个唯一的id,然后他们只需在成功回调中更改其背景网址。


第二次更新:

让我们为每个vote_upvote_down 链接分配一个唯一的ID:

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

然后你可以尝试用这个替换有问题的attr()调用:

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

上一个答案:

您正在尝试更改 span 元素的 src 属性。我猜应该是img,但你可以从选择器中省略标签名称:

$("#vote_buttons"+the_id).attr("src", "images/downvoteActive.png");

【讨论】:

  • 对不起,我是 jquery 新手,我不明白你刚才说什么
  • @gateway:用更多信息更新了我的答案:)
【解决方案2】:

您的选择器显示“span#vote_buttons”,而不是“img#vote_buttons”。基于“id”值的选择器不需要标签名称,通常最好不要使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多