Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery addClass()不工作_Jquery_Css_Class_Dom_Addclass - Fatal编程技术网

jQuery addClass()不工作

jQuery addClass()不工作,jquery,css,class,dom,addclass,Jquery,Css,Class,Dom,Addclass,我检查了关于这个问题的其他问题,但它们没有帮助我。我不明白为什么这不管用,但与其浪费时间试图弄清楚,我想我最好在这里问一下。我有以下AJAX调用: $("#day_list li").live("click", function() { var day = $(this).attr('value'); $.ajax({ type: "POST", url: "/planner/get_detail"

我检查了关于这个问题的其他问题,但它们没有帮助我。我不明白为什么这不管用,但与其浪费时间试图弄清楚,我想我最好在这里问一下。我有以下AJAX调用:

        $("#day_list li").live("click", function()  {
        var day = $(this).attr('value');
        $.ajax({
            type: "POST",
            url: "/planner/get_detail",
            data: { post_day: day, post_month: current_month, post_year: current_year },
            success: function(data)
            {
                $(this).addClass('selected');
                $(".detail_header").html(data['detail_header']);
            }
        });
    });
除了
$(this.addClass('selected'),这里的一切都很好。我的
。当前所选的
类如下所示:

.selected
{
     border: 1px solid red;
}
老实说,我不知道这里出了什么问题。一定是忽略了什么,但是什么?谢谢

试试这个:

$("#day_list li").live("click", function()  {
        var day = $(this).attr('value');
        var clickedObj = $(this);
        $.ajax({
            type: "POST",
            url: "/planner/get_detail",
            data: { post_day: day, post_month: current_month, post_year: current_year },
            success: function(data)
            {
                clickedObj.addClass('selected');
                $(".detail_header").html(data['detail_header']);
            }
        });
    });

由于javascript中的作用域,success中的$(this)不再反映单击的对象。相反,它反映了$.ajax对象。将其分配给ajax对象上方的变量可确保您对单击的对象有一个引用

您正在使用
this
作为选择器,但是
this
不会引用当前范围内的任何特定HTML元素。

我认为,当您在ajax请求中调用$(this)时,它会引用ajax对象,而不是点击所来自的元素(如果这有意义的话),我很愚蠢,因为我的列表项不会采用红色边框,但是当我尝试使用字体权重:粗体时,你的解决方案起了作用。非常感谢:D