jQuery - 仅针对单击元素进行更改,而不是所有具有相同类的更改

时间:2013-04-23 10:05:14

标签: javascript jquery

我想要做的是更改对象的属性,但仅限于那个。对于已点击的内容,$('.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 个答案:

答案 0 :(得分: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');
});

答案 1 :(得分: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节点。

答案 2 :(得分: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');
});

答案 3 :(得分: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');
});