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提供的方法操作。
作用:把当前对象保存起来,便于后边的使用。
最新文章
- C++内存对齐总结
- 让姑姑不再划拳 码农也要有原则 : SOLID via C#
- mount常用挂载命令
- Xcode6 管理provisioning profile
- [教学] Firemonkey 之 StringGrid Header 自订显示
- Unity3d发布成exe项目后的设置(全屏自适应屏幕大小)
- HDU 5405 (树链剖分+线段树)
- 使用讯飞SDK,实现文字在线合成语音
- HDU 5727 Necklace(二分图匹配)
- Spring学习之Jar包功能介绍(转)
- pojg2744找一个最长的字符串x,使得对于已经给出的字符串中的任意一个y,x或者是y的子串,或者x中的字符反序之后得到的新字符串是y的子串。
- Linux学习 -- Shell基础 -- Bash变量
- Java使用序列化的私有方法巧妙解决部分属性持久化问题
- WebApi系列~HttpClient的性能隐患 - 转
- brew装snappy
- zookeeper 杂记
- Centos7 JDK安装过程中 解决java -version 报错: bash: /home/jdk1.8.0_161/bin/java: Permission denied
- Oracle JDBC连接服务名、SID和tnsnames.ora配置的多种方式
- vs2017诊断工具
- C++之lambda理解
热门文章
- js文件的框架
- centos配置postfix邮件服务
- 分布式的几件小事(十一)分布式session如何实现
- mybatis-plus简单了解
- oracle数据泵expdp和impdp使用
- 让IE6、IE7、IE8、IE9、IE10、IE11支持Bootstrap的解决方法
- linux命令详解——yum
- FTP部署与使用
- 架构师成长之路5.4-Saltstack配置管理(LAMP架构案例)
- 解决 android studio 出现:";AndroidStudio:Could not GET 'https://dl.google.com Received status code 400 from server: Bad Request";问题