简体   繁体   English

下拉列表选择器JQUERY

[英]Drop down list selectors JQUERY

I'm trying to create a jQuery drop-down script that will be efficient and minimal. 我正在尝试创建一个高效且最小的jQuery下拉脚本。 I need a way to associate a couple of uls with a li and reference this in an each loop. 我需要一种方法来将几个ul与li关联起来,并在每个循环中引用它。 Think of each of the clickable li titles as a cluster and the 'child' ul's under them are part of this too. 将每个可单击的li标题都视为一个群集,它们下方的“子” ul也是其中的一部分。

I want to write a piece of efficient code, using an each loop to iterate through each of the clickable lis and expand the sublist uls associated with it. 我想编写一段高效的代码,使用一个each循环迭代每个可单击的lis,并扩展与之关联的子列表ul。 The problem is I need to know how to associate these sublist uls with the parent li in a way that can be referenced in an each loop. 问题是我需要知道如何以可在每个循环中引用的方式将这些子列表ul与父li关联。 No standard jquery selectors can really help me because the sublist uls are not REALLY children of the li. 没有标准的jquery选择器可以真正帮助我,因为子列表ul并不是li的真正子代。

HTML and Jquery attempt below. HTML和Jquery尝试如下。 Please Help 请帮忙

<ul>
        <li class='tier1'>This is what I want to click</li>
            <ul (#1)>
                <li>I want this to drop down</li>

            </ul>
                             <ul>
                <li  id="gap">I want these to drop down</li>

            </ul>
       <li class='tier1'>Another clickable title</li>
            <ul>
                <li  id="gap">I dont want this to drop down when I click the li at the top of this markup</li>

            </ul>
</ul>

My problem is that the ul (#1) that i want to slideToggle when I click the the li above it, is not quite a child. 我的问题是,当我单击它上方的li时,我想要slideToggle的ul(#1)还不是孩子。

So I made the following code: 所以我做了下面的代码:

$(document).ready(function(){
    $('.tier1').each(function(){
        $(this).click(function(){
            $(this).children('ul').slideToggle('slow');
        });
    });
});

Your main problem may be that in your provided HTML there is nothing with the "tier1" class. 您的主要问题可能是在您提供的HTML中,“ tier1”类没有任何内容。 You also don't need an .each() function, as you can just attach the click function to the class: 您也不需要.each()函数,因为您可以将click函数附加到类中:

$('.tier1').click(function(){
    $(this).children('ul').slideToggle('slow');
});

Your HTML was also invalid, you can't have UL's nested in UL's. 您的HTML也是无效的,您不能将UL嵌套在UL中。 Fixed version: 固定版本:

<ul>
    <li class="tier1">This is what I want to click
        <ul>
            <li>I want this to drop down</li>
        </ul>
        <ul>
            <li  id="gap">I want these to drop down</li>
        </ul>
    </li>
    <li class="tier1">Another clickable title
        <ul>
            <li id="gap">I dont want this to drop down when I click the li at the top of this markup</li>
        </ul>
    </li>
</ul>

You'll also want some CSS to hide the children before the click: 您还需要一些CSS在单击之前隐藏子级:

ul li ul{
    display: none;
}

Working demo in this fiddle . 这个小提琴中工作的演示。

See my example here: http://jsfiddle.net/D29mQ/2/ 在这里查看我的示例: http : //jsfiddle.net/D29mQ/2/

You have some errors in your markup. 您的标记中有一些错误。 A ul cannot be a direct descendent of a ul , but can be a direct descendent of a li . 一个ul不能是直接后裔ul ,但也可以是直接后裔li I have added a hide() on the ul 's within the li.drawer, so that the content is not hidden if javascript is disabled (I would not recommend hiding these with css). 我在li.drawer中的ul上添加了hide() ,这样,如果禁用了javascript,内容就不会被隐藏(我不建议使用CSS隐藏这些内容)。

<ul>
<li>
    <p class='handle'>This is what I want to click</p>
    <ul  class='drawer'>
        <li>
            <p class='handle'>This has a drawer too!</p>
            <ul  class='drawer'>
            <li>I want this to drop down</li>
            </ul>
        </li>
    </ul>
    <ul  class='drawer'>
        <li>I want these to drop down</li>
    </ul>
</li>

<li>
    <p class='handle'>This is what I want to click</p>
    <ul  class='drawer'>
        <li>I want this to drop down</li>
    </ul>
    <ul  class='drawer'>
        <li>I want these to drop down</li>
    </ul>
</li>
</ul>

<div>
    <p class='handle'>Here is an example using a wrapper div and paragraph tags.</p>
    <p  class='drawer'>
        I want this to drop down
    </p>
    <p  class='drawer'>
        I want these to drop down
    </p>
</div>  

and the jquery 和jQuery

$(document).ready(function(){
  $('.drawer').hide();
  $('.handle').click(function(){
        $(this).parent().children().not(this).slideToggle('slow');        
  });
});

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

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