+ -
当前位置:首页 → 问答吧 → $(this)在$.post的function里是无效的---这是为什么呢?

$(this)在$.post的function里是无效的---这是为什么呢?

时间:2010-01-05

来源:互联网

$('#del').click(function(){
    $.post(url,parm,function(){
         //some code 
         $(this).remove();      //在这里$(this)是无效的
    } );
});

 <a title="删除" id="del">删除</a>
    在这里$(this)在$.post的function里是无效的

作者: zhuzuoling   发布时间: 2010-01-05

$('#del').click(function(){
var $this = $(this);
    $.post(url,parm,function(){
         //some code
         $this.remove();
    } );
});

this的上下文变了,你的写法实际上是window对象

作者: keakon   发布时间: 2010-01-06

非常感谢1楼高人的帮助

作者: zhuzuoling   发布时间: 2010-01-06