简体   繁体   English

jQuery找到最接近的ul

[英]jQuery find closest ul

My HTML structure is like this: 我的HTML结构是这样的:

    <div class="product-slider-title">
        <div><span class="left">&nbsp;</span></div>
        <div>Sample title</div>
        <div><span class="right">&nbsp;</span></div>
    </div>
    <div id="prslider1" class="product-slider-wrapper">
        <div class="left-prduct-slider-nav sprites"></div>
        <div class="right-prduct-slider-nav sprites"></div>    
        <div class="product-slider">
            <ul>
                <li class="product-holder"></li>
            </ul>
        </div>
     </div>

How can i find the closest .product-slider ul from .left-prduct-slider-nav when i click on .left-prduct-slider-nav ? 我怎样才能找到最接近的.product-slider ul.left-prduct-slider-nav当我点击.left-prduct-slider-nav

if this is .left-prduct-slider-nav then 如果this.left-prduct-slider-nav

var ul = $(this).siblings('.product-slider').find('ul')

Demo: Fiddle 演示: 小提琴

You can use nextAll() to search the siblings after current element and use find() to searched the children in the matched element returned by nextAll . 您可以使用nextAll()搜索当前元素之后的兄弟姐妹,并使用find()搜索nextAll返回的匹配元素中的子元素。

Live Demo 现场演示

$(this).nextAll('.product-slider').find('ul');

这应该工作

var ul = $('.left-prduct-slider-nav').closest('ul');

Hmm try this: 嗯试试这个:

$('.left-prduct-slider-nav').click(function () {
    var closest_ul = $(this).parent().find('.product-holder ul');
});

Try first-child : 尝试first-child

HTML: HTML:

<div class="product-slider-title">
    <div><span class="left">&nbsp;</span>

    </div>
    <div>Sample title</div>
    <div><span class="right">&nbsp;</span>

    </div>
</div>
<div id="prslider1" class="product-slider-wrapper">
    <div class="left-prduct-slider-nav sprites">Left Slider Nav</div>
    <div class="right-prduct-slider-nav sprites"></div>
    <div class="product-slider">
        <ul>
            <li class="product-holder1"></li>
        </ul>
        <ul>
            <li class="product-holder"></li>
        </ul>
        <ul>
            <li class="product-holder"></li>
        </ul>
        <ul>
            <li class="product-holder"></li>
        </ul>
    </div>
</div>
<div class="product-slider-title">
    <div><span class="left">&nbsp;</span>

    </div>
    <div>Sample title</div>
    <div><span class="right">&nbsp;</span>

    </div>
</div>
<div id="prslider1" class="product-slider-wrapper">
    <div class="left-prduct-slider-nav sprites"></div>
    <div class="right-prduct-slider-nav sprites"></div>
    <div class="product-slider">
        <ul>
            <li class="product-holder"></li>
        </ul>
    </div>
</div>

JS: JS:

$(document).ready(function () {
    $(".left-prduct-slider-nav").click(function () {
        alert($(".product-slider").find("ul:first-child").find("li").prop("class"));
    });
});

jsFiddle. jsFiddle。

Explanation: 说明:

I handle the .click() function of .left-prduct-slider-nav then I alert the class of the closest or in other words the first ul:first-child 's li. 我处理了.left-prduct-slider-nav.left-prduct-slider-nav .click()函数,然后提醒最接近的class ,即第一个ul:first-child的li。

Proof: 证明:

The proof is that I changed the class of the first ul 's li and then alert() it and then the output I get is this: product-holder1 . 证据就是我改变了class第一的ullialert()然后输出我得到的是这样的: product-holder1

The closest .product-slider ul from .left-prduct-slider-nav is just the first one in the list, so you can use :first : .left-prduct-slider-nav .product-slider ul只是列表中的第一个,因此可以使用:first

$('.left-prduct-slider-nav').click(function() {

    $(this).parent().find('.product-slider ul:first')

})

尝试这个:

$(this).next().next().find('ul');

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

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