jQuery双对象构造函数-例如$($(this))
jQuery double object constructor - e.g. $($(this))
假设您有以下代码:
function name() {
$(this).css('background', 'red');
}
$('selector1').click(name);
$('selector2').click(function () {
name.call($(this).parent());
});
现在,当通过单击"selector1"调用函数时,this
是HTML对象,$(this)
是jQuery对象,但如果通过单击"selector2"调用函数,this
已经是一个jQuery对象了,那么$(this)
是什么呢?
我知道我可以做name.call($(this).parent()[0]);
之类的事情来获得HTML对象,但我的问题是,当你做$($(this))
或$($('selector'))
之类的事情时会发生什么?结果是什么?最重要的是,使用这样的结构有什么害处吗?
this
已经是jQuery对象时,$(this)
会创建jQuery对象的副本。
来自jQuery文档:
克隆jQuery对象
将jQuery对象传递给
$()
函数时,将创建该对象的克隆。这个新的jQuery对象引用了与初始对象相同的DOM元素。
相关文章:
- RegEx删除空属性?例如,如果(class=“”||class=“”)移除;否则就下课
- $(this).prop('property') vs. this.property
- Twitter Bootstrap typeahead:使用“this”获取上下文/调用元素
- 在另一个函数中使用变量this
- this.router在AngularJS 2中未定义
- reactjs this.refs vs document.getElementById
- JavaScript 中的嵌套函数和 “this” 关键字
- React+Meteor:this.ops返回未定义
- javascript中对象构造函数中的var属性与this.properties
- 函数中this和var之间的区别
- 在三个.js中创建球体(例如地球)的磁力线
- jQuery(this).Pparent().Pparente().Prent().find()在iPad上不起作用
- 任何将(SSH)终端嵌入到一些HTML5表示系统中的想法(例如,show.js)
- Understanding Javascript scope with "var that = this&qu
- 定义this.properties或objectName.properties的javascript JSON对象
- Object.prototype using 'this'
- 在JavaScript类型的函数中避免self-this
- 如何使用(this)访问Angular 2 http rxjs catch函数中的对象属性
- jQuery双对象构造函数-例如$($(this))
- 为什么调用带括号的方法,例如(obj.func)(),仍然设置“this”