使用闭包范围保留最后一个值 [英] Using the closure scope to keep the last value

查看:109
本文介绍了使用闭包范围保留最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

$('th').click(function() {
   var $th = $(this);
   ...
});

使用闭包范围,我想说:

Using the closure scope, I want to say:

var $th;
$('th').click(function() {
   if ($th !== $(this)) {
      $th = $(this);
      ...
   }
});

注意:该代码位于</body>之前,因此我不需要$(function(){});

Note: This code is just prior to </body>, so I won't need $(function() {});

推荐答案

您应检查基础DOM元素是否相等:

You should check whether the underlying DOM elements are equal:

if ($th[0] !== this) {

(您也可以在不调用$的情况下存储this本身)

(You could also store this itself without calling $)

这篇关于使用闭包范围保留最后一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆