在ajax回调中获取当前元素 $(this)

来源:互联网 发布:聚宝盆时时彩计划软件 编辑:程序博客网 时间:2024/06/04 18:57

1.起因:在ajax回调中获取当前元素使用$(this)显然是获取不到的。

2.解决方法:声明新的变量来获取该jquery对象

3.示例如下:

   ui.$gotoFocus.on('click', function() {     var thisObj = $(this);</span>      //获取关注好友的id:friendId      friendId = $(this).parents('li').data('id');      $.ajax({        url: oUrl.focusFriend,        type: 'POST',        dataType: 'jSON',        data: {         friend_id:friendId        },      })      .done(function(msg) {        if(0 == msg.code){          thisObj.removeClass('btn-gotofocus').addClass('btn-focused');</span>          thisObj.html('已关注');</span>        }      })      .fail(function() {        console.log("error");      })    });




0 0