我应该如何使用/计数 jQuery .find()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8733342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:58:09  来源:igfitidea点击:

How should I use/count jQuery .find()

jquerydomtraversal

提问by Ryan Durrant

I am using a jQuery plugin I found here: http://tutorialzine.com/2011/03/photography-portfolio-shutter-effect/except I have changed it a bit.

我正在使用我在此处找到的 jQuery 插件:http: //tutorialzine.com/2011/03/photography-portfolio-shutter-effect/,但我对其进行了一些更改。

It still works perfectly in the way it is supposed to, but I have added a next button in which the user cna use to skip to the next image, and it then pauses the interval for one loop so as not to changeonto the next picture too quickly.

它仍然以预期的方式完美运行,但我添加了一个下一个按钮,用户可以使用该按钮跳到下一张图片,然后暂停一个循环的间隔,以免切换到下一张图片迅速地。

However, my problem lies in a prvious button. I am not sure how to do it, so I have added a variable

但是,我的问题在于一个先前的按钮。我不知道该怎么做,所以我添加了一个变量

var positionInt

which is the position of the last list item so I know where I am in the list.

这是最后一个列表项的位置,所以我知道我在列表中的位置。

How do I use this along with the li object created by this line:

我如何将它与此行创建的 li 对象一起使用:

var container = $('#container'),
    li = container.find('li');

to open the correct li item?

打开正确的 li 项目?

Also, how can I find the length of the li object? I have tried li.size(), li.count() and li.length and none worked.

另外,如何找到 li 对象的长度?我试过 li.size()、li.count() 和 li.length 都没有奏效。

Or is there a way using just the .find() method to open the li item before the currently visible one? This is how it does it originally to go forward:

或者有没有办法只使用 .find() 方法在当前可见的项目之前打开 li 项目?这就是它最初的前进方式

li.filter(':visible:first').hide(); // Hid the only visible li
if(li.filter(':visible').length == 0){
li.show(); // if nothing is visible load a li (Im not really sure what this is pointing at, but it loads the next li)
}

Cheers

干杯

回答by Jules

You can get the number of li's like:

您可以获得li's的数量,例如:

var nr_li = $("#container ul li").length;

If you know the current index of the currently used <li>you can get the previous one by doing:

如果您知道当前使用的当前索引,则<li>可以通过执行以下操作获得前一个索引:

$prev_li = $("#container ul li:eq(" + (index-1) + ")");

Do make sure though that you do some calculations and checks on that index-1to make sure you don't go out of bounds and try reaching an unexisting element.

一定要确保你做了一些计算并检查它index-1以确保你不会越界并尝试到达一个不存在的元素。

Another thing you could do is cycle through the <li>'s and add each of them into an array for instance. You can then just pull the previous one out of your array.

您可以做的另一件事是循环遍历<li>' 并将它们中的每一个添加到数组中。然后,您可以将前一个从阵列中拉出。

var array_li = new Array();

$('#container li').each(function() {
   array_li.push($(this));
});

回答by James Montagne

Both lengthand size()will work to give you a count of the number of elements in a jquery object.

双方lengthsize()会努力给你一个jQuery对象的元素数量的计数。

http://jsfiddle.net/KeKPb/

http://jsfiddle.net/KeKPb/

I find that lengthis used more often as there is no need for the overhead of the size()call when all it does is use lengthinternally anyway.

我发现它length被更频繁地使用,因为size()当它所做的一切都是在length内部使用时,不需要调用的开销。

You'll need to define what "not working" means because both methods are perfectly valid.

您需要定义“不工作”的含义,因为这两种方法都是完全有效的。