Jquery $() - 它返回什么,什么是 $()[0]? [英] Jquery $() - what does it return, and what is $()[0]?
问题描述
我正在查看如何使用 jqGrid 的示例,这是一个 jQuery 插件.
I'm looking at an example of how to use jqGrid, which is a jQuery plugin.
它正在一个 div 中绘制一个 id 为list"的网格.
It's drawing a grid in a div with an id of 'list'.
它使用 $('#list').jqGrid(...)
创建网格.
但它用 $('#list')[0].addJSONData(...)
填充网格.
我一直在网上寻找有关 jQuery 的教程,试图了解其中的区别,但我没有发现任何东西可以解决对我来说是使用它的最基本问题.
I've been looking around the web for tutorials on jQuery, trying to understand the difference, and I've found nothing that addresses what is - to me - the most fundamental question in using it.
$()
返回什么?它是否返回一个包含 DOM 元素的 jquery 对象?它是否返回一个包含 DOM 元素数组的 jquery 对象?它是否返回添加了其他 jQuery 函数的 DOM 元素?
What does $()
return? Does it return a jquery object that contains a DOM element? Does it return a jquery object that contains an array of DOM elements? Does it return a DOM element to which additional jQuery functions have been added?
那么,$()[0]
是什么?如果 $()
返回一个包含 DOM 元素数组的 jQuery 对象,我希望它是具有 id 'list' 的 div,但 addJSONData
不是'一个 DOM 方法,它是一个 jqGrid 方法.jqGrid 是否将该方法添加到数组中的所有 DOM 元素?
And what then, is $()[0]
? If $()
returned a jQuery object that contained an array of DOM elements, I'd expect it to be the div with the id 'list', but addJSONData
isn't a DOM method, it's a jqGrid method. Does jqGrid add that method to all of the DOM elements in the array?
====== 添加 ======
===== ADDED ======
如果 $() 返回一个包含 DOM 对象数组的 jquery 对象,为什么 $()[0] 引用一个包含 addJSONData 方法的对象?addJSONData 不是 DOM 方法,而是 jqGrid 方法.
If $() returns a jquery object that contains an arrray of DOM objects, why does $()[0] refer to an object that contains an addJSONData method? addJSONData is not a DOM method, it's a jqGrid method.
推荐答案
$()
返回基于选择器的元素集合.所以$('.help')
将返回所有具有.help
类的元素.$('.help')[0]
会给你第一个元素.
$()
returns a collection of elements based on the selector. So $('.help')
would return all elements with a class of .help
. $('.help')[0]
would give you the first element.
这篇关于Jquery $() - 它返回什么,什么是 $()[0]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!