javascript - 跨度 addClass 不工作 ajax

标签 javascript jquery html ajax

我有这样的跨度 <span class="join-event to-join">join event</span> . 我使用此功能通过 AJAX 对点击进行操作。

但在成功功能中,我似乎无法向该点击范围添加一个类。

function accept() {

    $(".to-join").click(function() {

        var id = $(this).attr("data-event");

        $.ajax({

            type: "post",
            url: "../assets/js/ajax/toggle-events.php",
            data: { 'id': id },
            success: function() {

                alert("good job");
                $(this).addClass("joined");

            },

            error: function () {

                alert("fail");

            }

        });

    });

}

最佳答案

使用ajax方法的context选项:

context: this,

context Type: PlainObject This object will be the context of all Ajax-related callbacks. By default, the context is an object that represents the Ajax settings used in the call ($.ajaxSettings merged with the settings passed to $.ajax).

关于javascript - 跨度 addClass 不工作 ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31321730/

相关文章:

javascript - Node (keystonejs)地址已在使用错误,但不是。还有 EACCES 错误。 NGINX 落后

php - 我可以在不上传任何文件的情况下使用 Dropzone 提交表单吗?

jquery - 选择所有 li,但不选择子项

javascript - 选中/取消选中单选框后向 div 添加/删除一些文本

javascript - p5 : resize canvas, 具有固定的起始宽度/高度?

c# - 当我在表单 onsubmit 中有函数时,Ajax 不起作用

javascript - 使用Jquery获取表中可编辑td的值

html - 如何去掉 <details> 和 <summary> 标签周围的框架板?

javascript - Angular如何定义是否应该使用aot编译器

asp.net - 是否可以使用 Javascript 破坏由 ASP.NET (VB.NET) 设置的 session 变量?