简体   繁体   English

正确设置jQuery选择器的范围

[英]Properly setting the scope of a jQuery Selector

Hey guys I'm having a problem limiting the scope of a jQuery selector. 大家好,我在限制jQuery选择器范围方面遇到问题。 I've created a slideshow widget that depends on an unordered list for a structure as follows: 我创建了一个幻灯片显示小部件,该小部件依赖于结构的无序列表,如下所示:

<ul id="caption">
            <li class="visible">
                <p>
                   SwitchPoint Solutions is a leading provider of automated configuration solutions for telecommunications carriers globally.  
                   We offer services in the TDM network optimization, TDM to VoIP migration, and hosted PBX/Contact Centre areas.
                </p>
                <a href="#" class="button">Let's Go</a>
            </li>
            <li>
                <h2>TurboMove</h2>
                <p>
                    An automated optimization solution that helps carriers:
                      <li>Extend TDM network lifecycles</li>
                      <li>Decrease operating expenses (OPEX)</li>
                      <li>Decrease total cost of ownership (TCO)</li>
                      <li>Decrease carbon footprint</li>

                </p>
                <a href="#" class="button">Let's Go</a>
            </li>
            <li>
                <h2>Exodus</h2>
                <p>
                    Automates the data move during the of the migration TDM to VoIP.  Some of its main features are: automated data move, 
                    data integrity checks, and maintaining recent changes made by the subscriber.
                </p>
                <a href="#" class="button">Let's Go</a>
            </li>

there are more list elements but I didn't include them for brevity. 列表元素更多,但为简洁起见,我没有将它们包括在内。 Basically each caption is being switched using the visible class as a marker. 基本上,每个字幕都是使用可见类作为标记来切换的。 The actual code for the switching is as follows: 切换的实际代码如下:

   function autoSlideshow(mode) {
    var currentImage = $('#gallery li.visible');                                      //Determine which slide is visible
    var currentCaption = $('#caption li.visible');
    var currentSlide = $('#controls a.pagination.visible');     
    var transitionSpeed = 750;

    if(mode == -1){
        var nextImage = currentImage.next().length ? currentImage.next() :              //Determine the next slide
                    currentImage.siblings(':first');        
        var nextCaption = currentCaption.next().length ? currentCaption.next() :         
                    currentCaption.siblings(':first');
       var nextSlide = currentSlide.next().length ? currentSlide.next() :         
                    currentSlide.siblings(':eq(1)');
    }
    else{
        var nextImage = $('#gallery li:eq('+mode+')');
        var nextCaption = $('#caption li:eq('+mode+')');
        var nextSlide = $('#controls a.pagination:eq('+mode+')');
    }  

    currentImage.fadeOut(transitionSpeed).removeClass('visible');
    nextImage.fadeIn(transitionSpeed).addClass('visible');  
    currentCaption.fadeOut(transitionSpeed).removeClass('visible');
    nextCaption.fadeIn(transitionSpeed).addClass('visible');
   currentSlide.removeClass('visible');
    nextSlide.addClass('visible');

}       

The problem I'm having is that the second list element in the unordered list with the caption id has a nested list element in it which only displays the nested list one element at a time! 我遇到的问题是,带有标题ID的无序列表中的第二个列表元素中有一个嵌套列表元素,该嵌套列表元素一次只显示一个嵌套列表!

I'm assuming that I haven't limited the scope of this selector properly $('#caption li.visible'); 我假设我没有适当地限制此选择器的范围$('#caption li.visible'); but I haven't been able to figure out how to limit a selector to only select one level of the list. 但我一直无法弄清楚如何将选择器限制为仅选择列表的一个级别。 I'm sure this isn't something complicated by my newb-ish brain is not working it out. 我确定这不是复杂的事情,因为我的新大脑无法解决问题。

I'm not entirely sure what you mean by "one level" of the list. 我不能完全确定您所说的“一级”是什么意思。 If you want the first matched visible element you could do the following 如果您想要第一个匹配的可见元素,可以执行以下操作

$('#caption li.visible:first');

Or, providing you don't really need to qualify that it's inside caption or an LI 或者,如果您真的不需要限定字幕或LI内的内容

$('.visible:first');

You can use .children() to get the first level elements: 您可以使用.children()获取第一级元素:

$('#caption').children() or $('#caption').children('li') $('#caption').children()$('#caption').children('li')

If you go to the docs (the link above) they have an example with nested lists as well. 如果您去文档(上面的链接),他们也有一个带有嵌套列表的示例。

Also, to get the next element in the list you can simply do: 另外,要获取列表中的下一个元素,您只需执行以下操作:

var nextImage = currentImage.next();
var nextCaption = currentCaption.next();

Of course if you want it to loop back to the first one, you can detect that the current one is the last one and do: 当然,如果希望它循环回到第一个,则可以检测到当前的是最后一个,然后执行以下操作:

if(currentImage.next().length == 0) { 
  nextImage = currentImage.parent().children().first(); 
}

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

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