var $this = $(this)
jQuery: What’s the Difference Between $(this), $this, and this?
What about $this?
$this is a little different because it’s actually just a variable that uses the $. It has no inherent relation to jQuery.
It would be no different than JavaScript variables named $corn or $carrots.
You just was easily say var $this = “My pet cat is named Mittens.”; It’s just a variable with the dollar sign in it.
JavaScript allows characters like this in variable names.
The reason that you see use of $this inside of jQuery plugins is that often times developers in the global scope inside of their plugin will say something like:
var $this = $(this);
That way they can always have a reference to the object on which the plugin was called.
The scope of “this” will change in any methods used inside the plugin so it’s always good to have a global variable (that is, global inside the plugin scope) which you can refer to the object on which the plugin was called.
But the important thing to remember is that $this has no inherent relation to jQuery. It’s just a variable like any other.
What is the reason for var $this = this
Generally, this means a copy of this. The thing about this is that it changes within each function.
Storing it this way, however, keeps $this from changing whereas this does change.
jQuery heavily uses the magic this value.
Consider this code, where you might need something like you are seeing:
$.fn.doSomethingWithElements = function() {
var $this = this;
this.each(function() {
// `this` refers to each element and differs each time this function
// is called
//
// `$this` refers to old `this`, i.e. the set of elements, and will be
// the same each time this function is called
});
};
https://blog.csdn.net/mxt123456/article/details/53152398
在很多地方,我们都会看到
var $this = $(this)的代码,那它到底是什么意思,有什么用呢?
this其实是一个html 元素。
$this 只是个变量名,加$前缀是为说明其是个jquery对象。
而$(this)是个转换,将this表示的dom对象转为jquery对象,这样就可以使用jquery提供的方法操作。
作用:把当前对象保存起来,便于后边的使用。
var $this = $(this)的更多相关文章
- check environment var
田+R cmd set XXX check environment var
- var t = a&&b;的问题
var a = "avalue";var b = "bvalue";var t = a&&b;console.info(t); // bvalu ...
- js中的let和var
在ES6中,应该尽量使用const和let来声明变量,而尽量避免使用var. var的缺点是它的作用域比较混乱,使用let能够保证清晰的作用域. 下面看一个小例子. var x = 3; if(x== ...
- 【转载】了解CSS/CSS3原生变量var
文章转载自:鑫空间鑫生活(https://www.zhangxinxu.com/) 原文链接:http://www.zhangxinxu.com/wordpress/?p=5804 内容摘要: 在任何 ...
- 在开发中到底要不要用var?
var是.net的一个语法糖,在Resharper中推荐都使用这个关键字,平常我也是经常用:但是在跟其他程序员推广使用时,他的一些考虑引发了我的深思,到底该不该使用这个关键字呢? 我使用的理由 我使用 ...
- javaScript中的小细节-局部作用域中的var
javaScript中var是很神奇的,在局部作用域中,var a = b = c = 1;是不一样的,a为使用var声明的变量,而b和c则是全局下的,此类变量被称为隐式全局变量:var a = 1; ...
- get_object_vars($var) vs array($var)
get_object_vars(\(var) vs array(\)var) test case class Test { public function actionGetObjectVarsVsA ...
- var 的使用
List<Enterprise> epList = ViewBag.epList; foreach (var item in epList){ //todo ... } 当 List< ...
- 向量自回归模型VS风险价值模型(VAR&VaR)
单从外观上看,VAR&VaR两个模型很容易混淆,但就模型方法和用处两者截然不同,R语言作为数据分析的有力工具,其函数包库中包含各种各样的统计模型.通过vars包可以调用向量自回归模型,通过Pe ...
- JavaScript ES6 的 let 和 var 的比较
在JavaScript 1.7中, let 关键词被添加进来, 我听说它声明之后类似于"本地变量", 但是我仍然不确定它和 关键词 var 的具体区别. 回答: 不同点在于作用域, ...
随机推荐
- [转载]布隆过滤器(Bloom Filter)
[转载]布隆过滤器(Bloom Filter) 这部分学习资料来源:https://www.youtube.com/watch?v=v7AzUcZ4XA4 Filter判断不在,那就是肯定不在:Fil ...
- JQuery——关于CDN(内容分发网络)
替代方案 如果您不希望下载并存放 jQuery,那么也可以通过 CDN(内容分发网络) 引用它. Staticfile CDN.百度.又拍云.新浪.谷歌和微软的服务器都存有 jQuery . 如果你的 ...
- 2019-2020-1 20199319《Linux内核原理与分析》第九周作业
进程的切换和系统的一般执行过程 进程调度的时机 1.中断:起到切出进程指令流的作用.中断处理程序是与进程无关的内核指令流.中断类型: 硬中断:可屏蔽中断和不可屏蔽中断.高电平说明有中断请求. 软中断/ ...
- cmd 创建并写入文件
一.建立空文件的几种方法1.cd.>a.txtcd.表示改变当前目录为当前目录,即等于没改变:而且此命令不会有输出.>表示把命令输出写入到文件.后面跟着a.txt,就表示写入到a.txt. ...
- docker容器内安装 rz、sz
操作系统:ubuntu rz.sz命令找不到: 执行命令:apt-get update && apt-get install lrzsz
- spring常用的几个aware bean接口
BeanNameAware 作用:让Bean获取自己在BeanFactory配置中的名字(根据情况是id或者name). Spring自动调用.并且会在Spring自身完成Bean配置之后,且在调用任 ...
- 【AGC 036C】GP2
https://atcoder.jp/contests/agc036/tasks/agc036_c 题意 有一个长度为 $n$ 的非负整数序列 $x$,初始时全为 $0$.一次操作定义为选择一对正整数 ...
- uestc summer training #1
A 一个很好想的dp ll dp[maxn][]; int main() { scanf("%d%d",&n,&k); memset(dp,,sizeof(dp)) ...
- hive创建分区表
#创建分区表CREATE TABLE if not exists data_center.test_partition (id int,name string,age int)PARTITIONED ...
- 阿里云-docker安装mysql
1.检查内核版本,必须是3.10及以上 uname ‐r 2.安装docker yum install docker 3.输入y确认安装 4.启动docker:service docker start ...