简体   繁体   English

无法将类添加到单击的元素

[英]Cannot add class to clicked element

I have code for adding products into favorites using JQuery and AJAX, my JavaScript code looks like: 我有使用JQuery和AJAX将产品添加到收藏夹的代码,我的JavaScript代码如下所示:

$('.product-item-btn-fav').on('click', function(e){
    b = $(this).data("product_number");
    $.ajax({
        type: "POST",
        url: domain + "/ajax/favorite/" + b,
        success: function (a) {
            var d = parseInt($(a).find("#result").html());
            if (d == 1) {
                $(this).addClass("active");
            } else {
                if (d == -1) {
                    $(this).removeClass("active");
                }
            }
        }
    })
});

And HTML: 和HTML:

<a class="product-item-btn-fav" data-product_number="[%item.product_number%]">
    <svg class="svg-icon-heart-filled">
        <use xlink:href="[%domain.url_media%]/images/svg-sprite.svg#svg-icon-heart-filled"></use>
    </svg>
</a>

This code works, it adds product into favorite list at backend side (so AJAX works and it returns valid result 1 or -1), but this call $(this).addClass("active"); 这段代码有效,它将产品添加到后端的收藏夹列表中(因此AJAX可以工作并返回有效的结果1或-1),但是这个调用$(this).addClass("active"); doesn't add css class to <a> tag. 不会将<css类添加到< <a>标记。

You have to store $(this) in variable for a 你必须保存$(this)中的变量a

$('.product-item-btn-fav').on('click', function(e){
    b = $(this).data("product_number");
    var _t = $(this);
    $.ajax({
        type: "POST",
        url: domain + "/ajax/favorite/" + b,
        success: function (a) {
            var d = parseInt($(a).find("#result").html());
            if (d == 1) {
                _t.addClass("active");
            } else {
                if (d == -1) {
                    _t.removeClass("active");
                }
            }
        }
    })
});

this does not point the element you are thinking, store this in a variable and use that inside the ajax callback function: this不点,你所想的元素,存储this一个变量,并使用了AJAX回调函数内:

$('.product-item-btn-fav').on('click', function(e){
  var b = $(this).data("product_number");
  var prod = $(this);
  $.ajax({
    type: "POST",
    url: domain + "/ajax/favorite/" + b,
    success: function (a) {
      var d = parseInt($(a).find("#result").html());
      if (d == 1) {
        prod.addClass("active");
      } else {
          if (d == -1) {
            prod .removeClass("active");
          }
        }
      }
   });
});

That is because context to anchor element is lost in ajax callback function. 这是因为锚元素的上下文在ajax回调函数中丢失了。 You can set the context using context option in ajax. 您可以使用ajax中的context选项设置上下文。 See Ajax Docs : 请参阅Ajax文档

$.ajax({
    type: "POST",
    context : this,
    url: domain + "/ajax/favorite/" + b,
     success: function (a) {
        var d = parseInt($(a).find("#result").html());
        if (d == 1) {
            $(this).addClass("active");
        } else {
            if (d == -1) {
                $(this).removeClass("active");
            }
        }
    }
})
$.ajax({
    type: "POST",
    context : this,
    url: domain + "/ajax/favorite/" + b,
     success: function (a) {
        var d = parseInt($(a).find("#result").html());
        if (d == 1) {
            $(this).addClass("active");
        } else {            
                $(this).removeClass("active");
               }
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM