在jQuery中做什么……var myVar = $([]); ......做? [英] In jQuery what does...... var myVar = $( [] ); ......do?

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

问题描述

var myVar = $( [] );

此jQuery的作用是什么?

是否将变量初始化为空的jQuery集?我已经搜索了jQuery文档,但没有找到有关此语法的解释.

jQuery文档的摘录 http://api.jquery.com/jQuery/

返回空集-

从jQuery 1.4开始,调用不带参数的jQuery()方法将返回一个空的jQuery集(其length属性为0).在早期版本的jQuery中,这将返回一个包含文档节点的集合.

$( [] )可能是返回空jQuery集的遗留方法吗,还是做一些完全不同的事情?

解决方案

虽然有多种方法可以解释旧版"(如pst的评论中所述),但该表达式已被精确记录.我们知道$函数与jQuery相同,因此查找jQuery可以看到它可以接受几种不同形式的参数:

  • jQuery( selector, [context] )
  • jQuery( element )
  • jQuery( elementArray )
  • jQuery( jQueryObject )
  • jQuery()

一个接受元素数组的文件记录如下:

  • elementArray:包含一组要包装在jQuery对象中的DOM元素的数组.

这回答了您的问题"$( [] )可能是用于返回空jQuery集的旧方法吗,或者它会做完全不同的事情?"从某种意义上说,它没有任何不同.您将获得一个jQuery对象,其中包含空数组中的所有元素.全部都是零.

正如您在问题中提到的那样,从1.4版本开始,普通ol'$()会为您提供空集,因此我认为这可能是现在做事的首选方式.就是说,$([])很好并且很明确,并且仍然受支持.

var myVar = $( [] );

What does this jQuery do?

Does it initialize the variable to an empty jQuery set? I have searched the jQuery docs but haven't found an explaination for this syntax.

Excerpt from the jQuery docs http://api.jquery.com/jQuery/

Returning an Empty Set -

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

So could $( [] ) be a legacy method for returning an empty jQuery set or does it do something entirely different?

解决方案

While there are different ways to interpret "legacy" (as in pst's comment), the expression is precisely documented. We know that the $ function is the same as jQuery so looking up jQuery we see it can accept arguments in several different forms:

  • jQuery( selector, [context] )
  • jQuery( element )
  • jQuery( elementArray )
  • jQuery( jQueryObject )
  • jQuery()

The one accepting an element array is documented like so:

  • elementArray: An array containing a set of DOM elements to wrap in a jQuery object.

This answers your question "So could $( [] ) be a legacy method for returning an empty jQuery set or does it do something entirely different?" in the sense that no, it does not do anything different. You get a jQuery object with all the elements in your empty array. All zero of them.

And as you mentioned in your question, as of 1.4, plain ol' $() gives you the empty set and so I take it that it may be the preferred way to do things now. That said, $([]) is nice and explicit, and still supported.

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

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