gpt4 book ai didi

javascript - jQuery 双对象构造函数 - 例如$($(这个))

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:27:01 25 4
gpt4 key购买 nike

假设您有以下代码:

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)this 已经是一个 jQuery 对象时,创建一个 jQuery 对象的副本。

来自jQuery docs :

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.

关于javascript - jQuery 双对象构造函数 - 例如$($(这个)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12159562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com