jquery$(this)对象缺少问题

jquery $(this) object missing issue

本文关键字:问题 对象 this jquery      更新时间:2023-09-26

我对jQuery的$(this)对象有问题,使我错过了this元素代码:

 $('.star').click(function (){
    var id   = $(this).parent().attr('id').split('rating')[1];
    var vote = $(this).index() + 1;
    var ThisStar = $(this);
    alert(ThisStar.parent().html())
    $.ajax({
        type: 'POST',
        url:  EXECUTION_URL + 'ajax/rate.php',
        data: {'article' :id  ,'rate': vote},
        success:function(data,tt,ff){
            alert(ThisStar.parent().html())
        }
  });

第一个警报火灾:单击节点的父节点的真实内容第二次警报:无效!!!!为什么它被更改为ajax?还是其他原因?请告诉我这个问题的一个好的解决方案

好的,实际上我检查了我的代码,在ajax函数之后有两行删除了this对象中存储的节点,因为ajax函数是异步函数,所以在成功函数内部的代码之前执行的两行>>感谢所有

您的代码应该运行良好,请检查证据。唯一的区别是:

success:function(data,tt,ff){
    alert(ThisStar.parent().html())
}

与。

success:function(data,tt,ff){
    alert(ThisStar.parent().html())
}});