读书人

$.post之后就取不到$(this)节点了

发布时间: 2013-09-05 16:02:07 作者: rapoo

$.post以后就取不到$(this)节点了?


<script type="text/javascript">
$(document).ready(function () {
$(".user_sidebar").children("li").children("a").eq(7).addClass("cur");
$(".btn3").click(function () {
if (confirm("你确定要取消对" + $(this).parents(".p_follow").children(".right_box").children(".UInfo_box").children(".NickName").html() + "的关注吗?")) {
$.post("ashx/AttP/Attp.ashx", { uid: $(this).parents(".p_follow").attr("id") }, function (data, status) {
if (status == "success") {
if (data == "1") {
$(this).parents(".p_follow").remove();
}
else {
alert("取消关注失败");
}
}
});
}
else {
}
});
});
</script>

取消关注以后删除类标签为p_follow的父节点 结果在$.post以后就取不到$(this)节点了?是生命周期的问题么?还是什么?求指点~ jquery asp.net $.post 异步

$(this)值
[解决办法]
js中,new 出来function之后,this对象就变了

你要真想知道为什么,去看看jquery.post的源码就清楚了。
[解决办法]
google javascript this 指针

在作用域开头最好把以后要用的this指针存起来

a.click(function(){
var $this=$(this);
$.get("/a").always(
$this.val()
)
})
[解决办法]

引用:

<script type="text/javascript">
$(document).ready(function () {
$(".user_sidebar").children("li").children("a").eq(7).addClass("cur");
$(".btn3").click(function () {
if (confirm("你确定要取消对" + $(this).parents(".p_follow").children(".right_box").children(".UInfo_box").children(".NickName").html() + "的关注吗?")) {
$.post("ashx/AttP/Attp.ashx", { uid: $(this).parents(".p_follow").attr("id") }, function (data, status) {
if (status == "success") {
if (data == "1") {
$(this).parents(".p_follow").remove();
}
else {
alert("取消关注失败");


}
}
});
}
else {
}
});
});
</script>


取消关注以后删除类标签为p_follow的父节点 结果在$.post以后就取不到$(this)节点了?是生命周期的问题么?还是什么?求指点~


这其实是稍微“高级”一点的知识,不过2楼给的答案挺好的。
[解决办法]
#2楼正解。this在javascript里是不断变化的。如果在普通函数里,指向window对象;如果在按钮事件里指向按钮。应该象2楼一样,在按钮事件第一行保存按钮的引用,然后再绑定post之类的lamda函数时,就可以引用该变量了。

读书人网 >asp.net

热点推荐