gpt4 book ai didi

Jquery - 何时使用 "this"以及何时使用 "$(this)"?

转载 作者:行者123 更新时间:2023-12-03 22:08:40 25 4
gpt4 key购买 nike

Possible Duplicate:
jQuery $(this) vs this

“this”和“$(this)”有什么区别?

我如何知道使用哪一个?

我认为相关:

对于每个,您都有可选参数。“i”与“this”(或“$(this)”)有何不同?

$('img').each(function(i) { ....code }

对比

$('img').each(function() { ....code }

最佳答案

this 对象不会改变。它是函数的所有者。在大多数情况下,它只是一个节点,您可以引用它的所有属性,例如 this.className。 (将其想象为使用 document.getElementById 获得的节点或其他内容)。它只是函数的“所有者”。

因此,您只需将 this 对象传递给 jQuery 的 $()

结论:如果要对当前节点使用 jQuery 函数,请使用 $(this)。但如果您想访问对象自己的属性(例如 .nameclassName.id),只需使用 this.

关于Jquery - 何时使用 "this"以及何时使用 "$(this)"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8469635/

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