在jQuery中使用'this'变量fadeIn?

时间:2013-01-08 09:22:22

标签: javascript jquery this

我正在尝试使用ajax制作一个会喜欢该主题的按钮。但它必须只在淡出的线程上淡出和淡出受欢迎的图像。

我收到以下错误: Uncaught SyntaxError: Unexpected token this

那是我的代码第11行: $(this + ' .is_favorited').fadeIn("slow");

以下是完整的Javascript来源:

$(".do_favorite").live("click", function() {
    var item = $(this).closest(".box");
    var content = $(this).attr('data-id');
    alert(content);
    $.post( 'ajax.favorite.php?sid=' + content + '',
        $(this).serialize(),
        function(data) {
            if (data == "1") {
                // Favorite it
                $(this + ' .not_favorited').fadeOut("slow", function (
                    $(this + ' .is_favorited').fadeIn("slow");
                ));
            }else if (data == "2") {
                // Un-Favorite it
                $(this + ' .is_favorited').fadeOut("slow", function (
                    $(this + ' .not_favorited').fadeIn("slow");
                ));
            }else {
                alert("DER SKETE EN FEJL DU");
            }
        }
    );
    return false;
});

希望有人可以帮助我解决这个问题,因为我真的需要使用this,使其仅在点击的内容中淡出。

6 个答案:

答案 0 :(得分:4)

您需要在context选择器中传递this或使用$(this)上的 find() 功能。

更改

 $(this + ' .is_favorited').fadeOut("slow", function (

$('.is_favorited', this).fadeOut("slow", function (

使用在上下文后面调用的find()方法。

$(this).find('.is_favorited').fadeOut("slow", function (

修改

如果你想在post函数中使用类do_favorite引用事件源元素,那么你将它放到一些临时变量中,因为你不能在this中引用post

$(".do_favorite").live("click", function() {
    var item = $(this).closest(".box");
    var content = $(this).attr('data-id');
    alert(content);
    do_favorite_OBJECT = $(this);
    $.post( 'ajax.favorite.php?sid=' + content + '',
        do_favorite_OBJECT.serialize(),
        function(data) {
            if (data == "1") {
                // Favorite it
                $('.not_favorited', do_favorite_OBJECT).fadeOut("slow", function (
                    $('.is_favorited', do_favorite_OBJECT).fadeIn("slow");
                ));
            }else if (data == "2") {
                // Un-Favorite it
                $('.is_favorited', do_favorite_OBJECT).fadeOut("slow", function (
                    $('.not_favorited', do_favorite_OBJECT).fadeIn("slow");
                ));
            }else {
                alert("DER SKETE EN FEJL DU");
            }
        }
    );
    return false;
});

答案 1 :(得分:4)

你可以写成:

$(this).find('.not_favorited')

答案 2 :(得分:2)

像这样保存你的“这个”

    $(".do_favorite").live("click", function() {
        var me = $(this);
        var item = me.closest(".box");
        var content = me.attr('data-id');
        alert(content);
        $.post( 'ajax.favorite.php?sid=' + content + '',
        me.serialize(),
        function(data) {
            if (data == "1") {
            // Favorite it
            me.find(' .not_favorited').fadeOut("slow", function (
                me.find('.is_favorited').fadeIn("slow");
            ));
            }else if (data == "2") {
            // Un-Favorite it
            me.find('.is_favorited').fadeOut("slow", function (
                me.find('.not_favorited').fadeIn("slow");
            ));
            }else {
            alert("DER SKETE EN FEJL DU");
            }
        }
        );
        return false;
    });

答案 3 :(得分:1)

使用:

$(".is_favorited", this);

$(this).find(".is_favorited");

答案 4 :(得分:1)

this回调中的

$.post不是您认为的HTML元素。在调用$.post之前,您需要将元素保存在变量中。

答案 5 :(得分:1)

只需使用:

$(this).find('.not_favorited')