$([])在jQuery中意味着什么? [英] What does $([]) mean in jQuery?

查看:104
本文介绍了$([])在jQuery中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var username = $("#username"),
                password = $("#password"),
                allFields = $([]).add(username).add(password);

什么是allFields?什么是$([])?

What is allFields? What is $([])?

作为Javascript / jQuery的新手,我以前从未见过这个$([])表示法,我对它的相关方法很感兴趣。

Being a newbie to Javascript/jQuery, I've never seen this $([]) notation before and I'm interested in its associated methods.

鉴于它的$([]),搜索起来很棘手。谷歌在Javascript中搜索数组(猜测那个东西是某种数组)产生了我熟悉的典型数组。

Given that its "$([])", it's tricky to search for. And a Google search of arrays in Javascript (guessing that thing is an array of some sort) yields the typical arrays I'm familiar seeing.

那么什么是$([ ])?谁能指点我一些文件?我有兴趣学习如何使用这个奇怪的东西。

So what is $([])? Can anyone point me to some documentation? I'm interested in learning how to use this strange thing.

推荐答案

jQuery函数接受一个DOM节点数组。

The jQuery function accepts an array of DOM nodes.

$([document.body])例如,它将通过包装传入的所有DOM元素返回一个jQuery对象那个数组。但是,因为在你的例子中没有DOM对象开始,它只是一个空数组,所以甚至不需要传递一个空数组。

$([document.body]) for example which will return a jQuery object by wrapping all the DOM elements passed in that array. However, since in your example there is no DOM object to begin with and it's just an empty array, there is not need to even pass an empty array.

调用jQuery没有任何参数的函数返回一个空集。取自jQuery docs,

Calling the jQuery function without any arguments returns an empty set. Taken from jQuery docs,


返回空集

从jQuery 1.4开始,调用不带参数的jQuery()方法会返回一个空的jQuery集。在以前版本的jQuery中,这将返回一个包含文档节点的集合。

As of jQuery 1.4, calling the jQuery() method with no arguments returns an empty jQuery set. In previous versions of jQuery, this would return a set containing the document node.

所以,如果你有的话,你的例子会有相同的效果而是叫

So, your example would work the same if you had instead called

$().add(username).add(password);

正如其他答案所提到的那样,文档说,在v之前需要传递一个空数组1.4。

这篇关于$([])在jQuery中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆