jQuery - 父母"这个"

Ale*_*lex 8 jquery jquery-plugins jquery-selectors

$('form').each(function(){
  var form = this;
  $('a', this).click(function(){ form.submit(); });
});
Run Code Online (Sandbox Code Playgroud)

有没有办法this在不使用中间变量的情况下获取click函数内的parent()元素?我在想,也许有一个我不知道的关键字让你这么做.

在$ .ajax函数中有一个context参数允许你做这样的事情.

glo*_*tho 7

不,你已经做到了最好的方式.公约建议使用:

var that = this;
$('a', this).click(function(){ that.submit(); });
Run Code Online (Sandbox Code Playgroud)