调用show jQuery的对象数组 [英] Call show on array of jQuery objects

查看:150
本文介绍了调用show jQuery的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于 jQuery.show 的表现有点问题。
在IE8会出现此问题(下面也可能版本,但IE8是我的兴趣)。

I have a little problem concerning performance of jQuery.show. This problem occurs in IE8 (probably also versions below, but IE8 is of my interest).

我的jQuery对象的数组,让我们把它叫做元素
我想告诉他们,所以我所做的:

I have an array of jQuery objects, lets call it elements. I want to show them, so i did:

for (var i = elements.length - 1; i >= 0; i--) {
    elements[i].show();
}

瓶颈似乎是调用显示。该数组已经生成,使不花时间。通过数组循环不应该是一个问题,无论是。

The bottleneck seems to be the call to show. The array is already generated, so that takes no time. looping through an array should not be a problem either.

我想减少这种一个调用显示通过创建一个包含我所有元素的新的jQuery的元素。但我不知道如何做到这一点。我 jQuery.add 试图通过

I thought of reducing this to one call to show by creating a new jQuery element containing all my elements. but I was not sure how to do this. I tried by jQuery.add.

var $elements = elements[0];
for (var i = elements.length - 1; i >= 1; i--) {
     $elements = $elements.add(elements[i]);
}
$elements.show();

现在,这一次似乎与 jQuery.add 的一个问题。大概是因为它总是会创建一个新的对象。

Now, this time it seems to be a problem with jQuery.add. Probably because it always creates a new object.

所以,我认为,可以解决我的问题,有三种不同的方法。也许你可以帮我跟他们中的一个:

So, I thought of three different approaches that could solve my problem. Maybe you can help me with one of them:


  • 有没有像 jQuery的方法添加不返回一个新的对象,而是将其添加到当前jQuery的元素?

  • 有一个简单,快捷的方式的jQuery元素的数组?
  • 要创建一个jQuery元素
  • 有没有一种方法,以显示阵列中所有的jQuery对象以更快的方式?

  • Is there a jQuery method like add that does not return a new object, but instead adds it to the current jQuery element?
  • Is there an easy and fast way to create a jQuery element by an array of jQuery elements?
  • Is there a way to show all jQuery objects in an array in a faster way?

刚刚回答的问题之一,可能会帮助我在这里。我的问题是,jQuery的总是期望 DOM元素,而不是的数组jQuery的元素在其方法的数组。

Answering just one of the question would probably help me out here. My problem is, that jquery always expects an array of DOM elements and not an array of jQuery elements in its methods.

非常感谢事先!

- 编辑关于元素的含量

我有一个动态创建节点jstree(即元素)。这些元素具有数据-ID 属性来识别它们。
现在,我随时可以查询类似$(礼[数据-ID = XXX]'),但是这将是非常缓慢的。所以取而代之,当元素创建的,我将它们缓存到像对象字典(关键是数据的id,值是节点)。出于这种目的,我产生我的数组元素。发生这种情况非常快。
该阵列可具有尺寸高达4000节点。每jQuery的元素数组中只包含一个DOM的元素()。

I have a jstree that dynamically creates nodes (that is, li elements). These elements have an attribute data-id to identify them. Now, I could always query for something like $('li[data-id=xxx]'), but this would be very slow. So instead, when li elements are created, i cache them into a dictionary like object (key is the data-id, value is the node). Out of this object, i generate my array elements. This happens very fast. The array can have a size of up to 4000 nodes. Every jQuery Element in the array only contains one DOM-Element (li).

推荐答案

修改

阅读你的更新和评论,在此之后的非常的小单行是最有可能会是答案:

After reading your update, and the comments, this Very small one-liner is most likely going to be the answer:

elements.each($.fn.show);

使用JQ每个方法应用(当然,内部:呼叫被使用)显示方法把所有元素。

Using the jQ each method to apply (well, internally: call is used) the show method to all elements.

最简单的,并从jQuery的对象数组创建一个jQuery对象会是这样最快的方式:

The easiest, and fastest way to create a jQuery object from an array of jQuery objects would be this:

var objFromArr = $(jQarr);

我在控制台只是测试这一点:

I've just tested this in the console:

objFromArr = jQuery([jQuery('#wmd-input'),jQuery('#wmd-button-bar')]);
objFromArr.each(function()
{
    console.log(this);//logs a jQ object
    console.log(this[0]);//logs the DOMElement
});

不过话说回来:你的元素是JQ对象的数组,但如果它是一个选择的返回值( $('SomeClass的。')),那么实际上,它并非如此:在这种情况下,你的循环的的:

But having said that: you say elements is an array of jQ objects, but if it's the return value of a selector ($('.someClass')) then really, it isn't: in that case, your loop is "broken":

elements = $('.someClass');
for (var i=0;i<elements.length;i++)
{
    console.log(elements[i]);//logs a DOMElement, not a jQuery object
}
//It's the same as doing:
console.log(elements[0]);
//or even:
console.log($('.someClass').get(0));

但到最后,如果您有jQuery的对象的数组,你想调用的显示在方法的每个的各人其中,我怀疑这是最好的看法:

But in the end, if you have an array of jQuery objects, and you want to invoke the show method on each and every one of them, I suspect this is the best take:

elements.each($.fn.show);//use the show method as callback

这篇关于调用show jQuery的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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