jQuery双对象构造函数 - 例如$($(本)) [英] jQuery double object constructor - e.g. $($(this))

查看:71
本文介绍了jQuery双对象构造函数 - 例如$($(本))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有以下代码:

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对象传递给 $() 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆