码迷,mamicode.com
首页 > Web开发 > 详细

转: jquery中ajax回调函数使用this

时间:2018-11-08 20:47:26      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:function   code   作者   https   ora   lin   代码   lan   targe   

原文地址:jquery中ajax回调函数使用this

写ajax请求的时候success中代码老是不能正常执行,找了半天原因。代码如下

1 $.ajax({type: ‘GET‘,
2              url: "/flag/",
3              data: dat,
4              success:function(){
5                  $(this).prevAll(‘p‘).css("text-decoration","line-through");
6              }
7       });

最后发现是ajax中的回调函数(success等)直接用this不灵,解决办法是使用bind(this)绑定this到当前事件。

1 $.ajax({type: ‘GET‘,
2          url: "/flag/",
3          data: dat,
4          success:function(){
5              $(this).prevAll(‘p‘).css("text-decoration","line-through");
6          }.bind(this)
7          });

 

---------------------
作者:Lius153
来源:CSDN
原文:https://blog.csdn.net/Lius153/article/details/70145529
版权声明:本文为博主原创文章,转载请附上博文链接!

 

转: jquery中ajax回调函数使用this

标签:function   code   作者   https   ora   lin   代码   lan   targe   

原文地址:https://www.cnblogs.com/mumu122GIS/p/9931766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!