#39;$(this)#39;和#39;this#39;有什么区别?
我目前正在研究本教程: jQuery入门
$("#orderedlist").find("li").each(function (i) {$(this).append(" BAM! " + i);
});
$("#reset").click(function () {$("form").each(function () {this.reset();});
});
注意在第一个示例中,我们使用$(this)
在每个li
元素内附加一些文本。 在第二个示例中,我们在重置表单时直接使用this
。
我的猜测是在第一个示例中, $()
将每个li
元素转换为一个能够理解append()
函数的jQuery对象,而在第二个示例中, reset()
可以直接在表单上调用。
#1楼
是的,使用jQuery时只需要$()
。 如果您想要jQuery的帮助来做DOM事情,请记住这一点。
$(this)[0] === this
基本上,每次您返回一组元素时,jQuery都会将其转换为jQuery对象 。 如果您知道只有一个结果,它将在第一个元素中。
$("#myDiv")[0] === document.getElementById("myDiv");
#2楼
是的,通过使用$(this)
,您为该对象启用了jQuery功能。 仅通过使用this
,它仅具有通用Javascript功能。
#3楼
是的,您需要$(this)
用于jQuery函数,但是当您要访问不使用jQuery的元素的基本javascript方法时,只需使用this
。
#4楼
使用jQuery
,通常建议使用$(this)
。 但是,如果您知道(应该学习并知道)区别,那么有时使用this
会更方便快捷。 例如:
$(".myCheckboxes").change(function(){ if(this.checked) alert("checked");
});
$(".myCheckboxes").change(function(){ if($(this).is(":checked")) alert("checked");
});
#5楼
this
参考一个javascript对象和$(this)
用于封装用jQuery。
// Getting Name and modify css property of dom object through jQuery
var name = $(this).attr('name');
$(this).css('background-color','white')// Getting form object and its data and work on..
this = document.getElementsByName("new_photo")[0]
formData = new FormData(this)// Calling blur method on find input field with help of both as below
$(this).find('input[type=text]')[0].blur()//Above is equivalent to
this = $(this).find('input[type=text]')[0]
this.blur()//Find value of a text field with id "index-number"
this = document.getElementById("index-number");
this.valueor this = $('#index-number');
$(this).val(); // Equivalent to $('#index-number').val()
$(this).css('color','#000000')
#6楼
this
是元素, $(this)
是使用该元素构造的jQuery对象
$(".class").each(function(){//the iterations current html element //the classic JavaScript API is exposed here (such as .innerHTML and .appendChild)var HTMLElement = this;//the current HTML element is passed to the jQuery constructor//the jQuery API is exposed here (such as .html() and .append())var jQueryObject = $(this);
});
范围是指当前的执行上下文ECMA 。 为了理解this
,重要的是要了解执行上下文在JavaScript中的运行方式。
当控制进入的执行上下文(代码是在该范围内被执行)对于变量的环境被建立(词汇和可变环境 - 基本上此设置了一个区域变量的输入,其已经访问,且为局部变量是一个区域存储的),并且结合this
发生。
执行上下文形成逻辑堆栈。 结果是,堆栈深处的上下文可以访问以前的变量,但是它们的绑定可能已更改。 每次调用jQuery的一个回调函数,它改变了这个使用绑定 apply
MDN 。
callback.apply( obj[ i ] )//where obj[i] is the current element
调用apply
的结果是在jQuery回调函数内部, this
是指回调函数正在使用的当前元素 。
例如,在.each
,常用的回调函数允许.each(function(index,element){/*scope*/})
。 在此范围内, this == element
为true。
#7楼
因此,基本上,在$(this)
,您只是将$()
的this
传递为参数,以便可以调用jQuery方法和函数。
#39;$(this)#39;和#39;this#39;有什么区别?相关推荐
- bundle install 出现 #39;gem install mysql2 -v #39;0.3.15#39; succeeds before bunding #39;
bundle install 出现 'gem install mysql2 -v '0.3.15' succeeds before bunding ' 解决:sudo apt-get install ...
- firefox 39 linux,Mozilla Firefox 39.0 Beta 4 发布下载
Mozilla Firefox 39.0 Beta 4 今日发布于Mozila的FTP,目前官方还没有发布相关的更新报告,但从面向开发者的信息看,SSLv3被彻底移除出Firefox的支持范围,并实现 ...
- Property with #39;retain (or strong)#39; attribute must be of object type
AFNetworking 2.0 当Deployment Target 低于6.0时,AFURLConnectionOperation.h,AFURLSessionManager.h @propert ...
- 怎么发现RAC环境中#39;library cache pin#39;等待事件的堵塞者(Blocker)?
怎么发现RAC环境中的'library cache pin'等待事件的堵塞者(Blocker) 參考自 How to Find the Blocker of the 'library cache pi ...
- 2018-03-28 从人机交互角度看中文编程:#39;打开微信#39;
前文通用型的中文编程语言探讨之一: 高考是基于现有英文编程语言的一个技术思路. 在这个回答以及下面的讨论中, 又提到了编程其实是人机交互的一种形式. 不禁试着跳出程序员视角看这个问题. 几年前才发现M ...
- 如何像在SQL中一样使用#39;in#39;和#39;not in#39;过滤Pandas数据帧
本文翻译自:How to filter Pandas dataframe using 'in' and 'not in' like in SQL How can I achieve the equiv ...
- 【Bug Fix】Error : Can#39;t create table #39;moshop_1.#sql-534_185#39; (errno: 150)
运行alter操作, alter table xx_shop_info add index FK9050F5D83304CDDC (shop_area), add constraint FK9050F ...
- SVN Working copying #39;xxxxx/xxxx/xxxx#39; locked
使用SVN想更新上传时提示Working copying 'xxxx/xxxx/xxx' locked 解决方法: 选中你的项目,上面工具栏一行找到 Action → Cleanup all Wor ...
- 自我学习39:接口自动化测试用例功能测试用例区别
标题接口文档三要素 接口概述(接口实现功能.URL地址.GET/POST.接口使用场景描述) 接口入参 (Params/Body .Hearders) 接口出参(Json或CSS或其他格式) 标题接口 ...
最新文章
- Cocos2d-x3.2 屏幕截图
- 自己封装js组件 - 中级
- k8s和harbor的集成_爱威尔-基于kubernetes集群的项目持续集成(gitlab+harbor+Jenkins)安装...
- AD 画图 镜像翻转元器件
- C#省市二级联动(王者荣耀挑选英雄为例)
- dog log 算子_DoG和LoG算子
- 客户端脚本调用服务器端动态内容,移动到链接显示预览
- 标准IO库fgets和fputs对一个文本文件的读写操作
- WIN10系统和压缩内存占用磁盘过高的解决方案(亲测有效)
- armboot和内核文件系统烧写总结
- Java并发编程的艺术笔记(四)——ThreadLocal的使用
- C# 异步查询数据库(第二版)
- Java二级知识总结
- 旅游网站php源码,基于ThinkPHP框架开发的青春旅行旅游门户整站PHP源码
- 一个简单的姓名生成器
- AllenNLP入门笔记(一)
- 单因素试验固定效应模型方差分析
- wget 命令的使用:HTTP文件下载、FTP文件下载
- 解决:卸载anaconda后 cmd闪退或打不开
- python饼状图显示其比例_python可视化:matplotlib绘制的饼状图你了解吗?
热门文章
- java里css查找快捷键_CSS入门2—元素快捷键
- cad图形如何导入到奥维地图_将设计线路CAD图导入到奥维互动地图的方法
- 有关线程的相关知识(下)
- 仿IPhone滚轮组件分析WheelScroller
- linux除了eeprom其他的保存方法,linux的EEPROM的读写控制.doc
- access查询5月份出生_Access and VBA - 5分钟概览
- uniapp在安卓和IOS端 APP固定竖屏及获取设备信息
- float浮动的学习
- 关于读取数据库进行数据处理的一些小问题
- [LeetCode] Implement strStr()