我遇到了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函数是异步函数,所以在代码之前执行了两行在成功功能内>>谢谢所有
答案 0 :(得分:0)
您的代码应该可以正常运行, check the proof 。唯一的区别是:
success:function(data,tt,ff){
alert(ThisStar.parent().html())
}
VS
success:function(data,tt,ff){
alert(ThisStar.parent().html())
}});