阿贾克斯(这个)不工作 [英] Ajax (this) not working

查看:98
本文介绍了阿贾克斯(这个)不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当试图访问.box的类$容器,使用(这个)内的Ajax调用不起作用。

When attempting to access the '.box' class of the $container, using (this) inside of the ajax call doesn't work.

$container.on(
        "click",
        ".box",
        function(event){
            var description;
            if($(this)[0].style.width == '70%'){
                $(this).find(".resultData").fadeOut('slow');
                $(this).css('width', '18%');
            }else{
                $.ajax({
                    url:'scripts/php/fetchResultsData.php',
                    data: {action:value},
                    type: 'post',
                    dataType: 'json',
                    success: function(data){
                        description = data[0];
                        $(this).css('width', '70%');
                        $(this).append("\
                            <div class='resultData'>\
                                <div class='resultName'>" + $(this).find("p").html() + "</div>\
                                <div class='resultDesc'>" + description +"</div>\
                            </div>");
                        /*alert($(this).find("p").html());*/
                    }
                    })
            }
            $container.masonry('reload');
        }
    );

在情况下,它并不清楚我想要做的,我试图改变动态元素的CSS。但是,例如,

In case it's not clear what I'm trying to do, I'm attempting to change the css of dynamic elements. But for example,

$(本)的CSS('宽','70%');

是不是调整的CSS的。如果我移动了ajax,成功的部分之外,它的工作原理,但我无法得到描述。

Isn't adjusting the css at all. If I move it outside the ajax,success section, it works, but then I'm unable to get 'description'.

推荐答案

您正在接近。 '这'在你使用它的上下文中,指的是Ajax请求,而不是发出该事件的事。为了解决这个问题,使得AJAX请求之前保存一份本:

You're close. 'this' in the context you are using it, refers to the ajax request rather than the thing that issued the event. To fix this, store a copy of this before making the ajax request:

                   }else{
                        var me = this;
                        $.ajax({
                            ...
                            success: function(data){
                                description = data[0];
                                $(me).css('width', '70%');

这篇关于阿贾克斯(这个)不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆