失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > jquery中ajax回调函数使用this

jquery中ajax回调函数使用this

时间:2020-10-03 16:18:19

相关推荐

jquery中ajax回调函数使用this

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

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

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

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

如果觉得《jquery中ajax回调函数使用this》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。