1

总的来说,我是 jQuery 和 web 开发的超级菜鸟,需要一些帮助来实现 apprise 警报系统。我意识到“验证”函数中的函数无法识别“this”选择器,我应该实现 $.proxy 以从当前范围之外获取“this”选择器。但无论我尝试什么,我都无法让它发挥作用

$('.closebtn').click(function(){

            apprise('<center>Are you sure you want to delete this section?<br>This action can not be undone!</center>',{'verify':true}, function(r){
                if(r){
                     $(this).remove();
                }
            })
 })

提前感谢您的帮助

4

1 回答 1

1

很简单,存储this

$('.closebtn').click(function(){
  var $button = $(this);

  apprise(...., function(r){
    if (r){
      // re-reference it
      $button.remove();
    }
  });
});

您可能会看到 javascript 中的“类”也执行与此类似的操作(通常命名为self)。例如

var myObject = function(){
  var self = this;

  var MyMethod = function(){
    // can use "self" in here to reference the object
  };
};
于 2012-04-25T23:20:03.337 回答