jQuery - 父级 "this"

标签 jquery jquery-plugins jquery-selectors

$('form').each(function(){
  var form = this;
  $('a', this).click(function(){ form.submit(); });
});

有什么方法可以在不使用中间变量的情况下获取该单击函数内的父(this)元素吗?我在想也许有一个我不知道的关键字可以让你做到这一点。

在 $.ajax 函数中,有一个 context 参数,它允许您执行类似的操作。

最佳答案

不,你已经做到了最好的方式。惯例建议使用:

var that = this;
$('a', this).click(function(){ that.submit(); });

关于jQuery - 父级 "this",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6286023/

相关文章:

jQuery:用 .each() 替换字符串

javascript - Bootstrap 4 轮播 : make the slides at the left and right of the active slide visible

javascript - JQuery:自定义属性关键字选择器

jquery-plugins - 是否可以使用 Owl Carousel 实现圆形/无限轮播?

javascript - 对 jQuery 插件的操作始终是最后的选择?

javascript - 如何在 jQuery 中定义条件属性选择器?

javascript - 在不刷新查询页面的情况下加载某个 div

JavaScript:隐藏下拉列表中除一个之外的元素

javascript - jQuery ajax数据发布问题

jquery - 未捕获的类型错误 $(...)textillate 不是函数