Turn '$(this)' jquery to javascript 'this'

Turn '$(this)' jquery to javascript 'this'

本文关键字:this to javascript jquery Turn      更新时间:2023-09-26

是否可以将jquery '$(this)'转换为javascript 'this'?

如何做到这一点?

jQuery是基于的集合,并允许您使用索引符号([])或get方法访问该集合的元素。

如果你有一个jQuery集合:

var set = $(this);

…您可以通过set[0]set[set.length - 1],通过set.get(0)set.get(set.length - 1)访问它的元素(get也支持从末尾开始索引的负索引)。

$(this)的情况下,当然,您根本不需要使用$()  —直接使用this即可。无论您使用的是原始DOM引用,无论是this还是event.target,都是如此。

jQuery$主要只是一个返回jQuery对象的包装器函数。因此,删除$是所有你需要得到正常的DOM元素this

this // DOM ELement
$(this) // jQuery object
this // Again just a DOM Element

你不需要再做什么了。this将永远是原始的DOM元素,这是因为你包装它,它返回给你一个jQuery对象。

jQuery提供了.get,它也做同样的事情,但如果this是一个集合,则提供了获取特定元素的方便。