jQuery双对象构造函数 - 例如$($(本)) [英] jQuery double object constructor - e.g. $($(this))
问题描述
假设您有以下代码:
function name() {
$(this).css('background', 'red');
}
$('selector1').click(name);
$('selector2').click(function () {
name.call($(this).parent());
});
现在,当点击'selector1'来调用该函数时这个
是一个HTML对象, $(this)
一个jQuery对象,但如果通过单击'selector2'调用该函数这个
已经是一个jQuery对象,那么什么是 $(this)
?
Now, when the function is called by clicking on 'selector1' this
is an HTML object and $(this)
a jQuery object, but if the function is called by clicking on 'selector2' this
is already a jQuery object so what is $(this)
?
I知道我可以做一些类似 name.call($(this).parent()[0]);
来获取HTML对象,但我的问题是当你发生什么时会发生什么做一些像 $($(this))
或 $($('selector'))
?结果是什么,最显着的是,使用这样的结构有什么害处?
I know I could do something like name.call($(this).parent()[0]);
to get an HTML object, but my question is what happens when you do something like $($(this))
or $($('selector'))
? What is the result of that and, most impotently, is there any harm in using such a construct?
推荐答案
$(this)
当这个
已经是一个jQuery对象时,它就是同一个jQuery对象。
$(this)
when this
is already a jQuery object is just the same jQuery object.
来自 jQuery docs :
克隆jQuery对象
当jQuery对象传递给
$()$ c $时c> function,创建对象的克隆。
这个新的jQuery对象引用与初始对象相同的DOM元素。
Cloning jQuery Objects
When a jQuery object is passed to the
$()
function, a clone of the object is created. This new jQuery object references the same DOM elements as the initial one.
这篇关于jQuery双对象构造函数 - 例如$($(本))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!