jQuery的:eq()vs get() [英] jQuery : eq() vs get()
问题描述
我是jQuery的新手,我想知道jQuery的get()
和eq()
函数之间有什么区别.我可能会误解get()
函数的作用,但是我以为我无法在同一行中的返回元素上的返回元素上调用函数,这很奇怪.
I'm new to jQuery, and I'm wondering what the difference is between jQuery's get()
and eq()
functions. I may misunderstand what the get()
function does, but I thought it odd that I couldn't call a function on the returned on the returned element in the same line.
//Doesn't work
I.e. $("h2").get(0).fadeIn("slow");
//Works
$("h2").eq(0).fadeIn("slow");
推荐答案
.get()
和.eq()
都从jQuery对象数组返回单个元素",但是它们以不同形式返回单个元素.
.get()
and .eq()
both return a single "element" from a jQuery object array, but they return the single element in different forms.
.eq()
将其作为jQuery对象返回,这意味着DOM元素被包装在jQuery包装器中,这意味着它接受jQuery函数.
.eq()
returns it as a jQuery object, meaning the DOM element is wrapped in the jQuery wrapper, which means that it accepts jQuery functions.
.get()
返回原始DOM元素的数组.您可以像访问原始DOM元素一样通过访问它们的属性并调用其功能来操纵它们.但是它失去了作为jQuery包装对象的身份,因此.fadeIn
之类的jQuery函数将无法正常工作.
.get()
returns an array of raw DOM elements. You may manipulate each of them by accessing its attributes and invoking its functions as you would on a raw DOM element. But it loses its identity as a jQuery-wrapped object, so a jQuery function like .fadeIn
won't work.
这篇关于jQuery的:eq()vs get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!