如何创建一个空的jQuery结果

编程入门 行业动态 更新时间:2024-10-11 01:16:11
本文介绍了如何创建一个空的jQuery结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

编辑:从jQuery 1.4开始,使用 $()将按照以下说明运行。

Edit: As of jQuery 1.4, using $() will work as described below.

我需要遍历一个数组并创建一些我想要的元素一个jQuery结果对象。

I need to loop through an array and create a number of elements which I want to have in a single jQuery result object.

for (var i = 0; i < 10; ++i) { $myJQueryObj = $myJQueryObj.add($("<span>blahblah</span>")); }

然而,问题在于你需要一个jQuery对象来开始有了,你显然想把它弄空。在上面的示例中,我应该如何初始化 $ myJQueryObj ?

The problem with this, however, is that you need a jQuery object to begin with, and you obviously want to start it empty. In the above example, how should I initialise $myJQueryObj ?

以下示例不起作用,因为它们全部选择文档对象:

The following examples do not work, as they all select the document object:

$('') $() $(null) $(false)

这些确实有效......但......

These do work... but...

$('#nonExistantElement') // yuck $().slice(0,0) // surely there's a nicer way?

有更好的方法吗?

推荐答案

是的。尝试 $([])。 $()的原因不起作用是因为jQuery需要一个上下文,并且没有任何提供,将默认为 document 作为上下文。很多事情都依赖于这个假设是正确的,所以改变 $()意味着给我空集将充其量只是问题。

Yep. Try $([]). The reason $() doesn't work is because that jQuery expects a context, and without any supplied, will default to document as the context. Many things depend on this assumption being true, so changing $() to mean "give me the empty set" would be problematic at best.

更多推荐

如何创建一个空的jQuery结果

本文发布于:2023-10-12 04:08:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483678.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:创建一个   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!