3

我想做的是改变一个对象的属性,但只针对那个。因此,$('.up img').attr("src","img/up.png")对于已单击的元素,并非所有具有.up img.


jQuery:

$('.up img').click( function(){
        var postDataUp = $(this).attr('mod');
        $.post('/votePost.php', {varUp: postDataUp}, function(o){
            console.log(o);
            if(o == 1){
                $('.up img').attr("src","img/up.png");
            }else if(o == 2){
                $('.up img').attr("src","img/up-g.png");
                $('.down img').attr("src","img/dw.png");
            }else if(o == 3){
                $('.up img').attr("src","img/up.png");
            }
        }, 'json');
});
4

4 回答 4

6

$(this)改为仅针对单击的该类的特定元素,而不是使用类选择器。

$('.up img').click( function(){
        var postDataUp = $(this).attr('mod');
        $.post('/votePost.php', {varUp: postDataUp}, function(o){
            console.log(o);
            if(o == 1){
                $(this).attr("src","img/up.png");
            }else if(o == 2){
                $(this).attr("src","img/up-g.png");
                $('.down img').attr("src","img/dw.png");
            }else if(o == 3){
                $(this).attr("src","img/up.png");
            }
        }, 'json');
});
于 2013-04-23T10:06:34.867 回答
4

您可以更改代码以针对特定元素。

$('.up img').click( function(){
    var postDataUp = $(this).attr('mod');

    // get the specific element that you have clicked.
    // i use the $ before the name to easily identify jquery elements.
    var $elm = $(this);

    $.post('/votePost.php', {varUp: postDataUp}, function(o){
        console.log(o);
        if(o == 1){
            // targeting the specific element
            $elm.attr("src","img/up.png");
        }else if(o == 2){
            // targeting the specific element
            $elm.attr("src","img/up-g.png");
            $('.down img').attr("src","img/dw.png"); // not sure if you want to target individual element here?
        }else if(o == 3){
            // targeting the specific element
            $elm.attr("src","img/up.png");
        }
    }, 'json');
});

如果您要定位$('.down img')特定于被单击元素的元素,那么您可以使用该$elm元素然后遍历 DOM 节点。

于 2013-04-23T10:10:50.937 回答
0
$('.up img').click( function(){
    var $this=$(this);
    var postDataUp = $this.attr('mod');
    $.post('/votePost.php', {varUp: postDataUp}, function(o){
        console.log(o);
        if(o == 1){
           $this.attr("src","img/up.png");
        }else if(o == 2){
            $this.attr("src","img/up-g.png");
            $this.attr("src","img/dw.png");
        }else if(o == 3){
            $this.attr("src","img/up.png");
        }
    }, 'json');
});
于 2013-04-23T10:13:26.750 回答
0

尝试这个 -

$('.up img').click( function(){
        var postDataUp = $(this).attr('mod');
        var clicked = $(this);
        $.post('/votePost.php', {varUp: postDataUp}, function(o){
            console.log(o);
            if(o == 1){
                $(clicked).attr("src","img/up.png");
            }else if(o == 2){
                $(clicked).attr("src","img/up-g.png");
                $('.down img').attr("src","img/dw.png");
            }else if(o == 3){
                $(clicked).attr("src","img/up.png");
            }
        }, 'json');
});
于 2013-04-23T10:07:09.880 回答