简体   繁体   English

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

[英]How should I use/count jQuery .find()

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. 它仍然以它应该的方式完美地工作,但是我添加了一个下一个按钮,用户cna用它跳到下一个图像,然后它暂停一个循环的间隔,以便不改变下一个图片很快。

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? 打开正确的李项目?

Also, how can I find the length of the li object? 另外,如何找到li对象的长度? I have tried li.size(), li.count() and li.length and none worked. 我尝试过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? 或者是否有一种方法只使用.find()方法在当前可见的项目之前打开li项目? This is how it does it originally to go forward : 这是怎么回事它最初前进

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 干杯

You can get the number of li 's like: 你可以得到li的数量:

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-1 to 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. 你可以做的另一件事是循环遍历<li>并将它们中的每一个添加到一个数组中。 You can then just pull the previous one out of your array. 然后,您可以从阵列中拉出前一个。

var array_li = new Array();

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

Both length and 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 length is used more often as there is no need for the overhead of the size() call when all it does is use length internally anyway. 我发现length被更频繁地使用,因为不需要size()调用的开销,而它无论如何都在内部使用length

You'll need to define what "not working" means because both methods are perfectly valid. 您需要定义“不工作”的含义,因为这两种方法都是完全有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM