简体   繁体   English

如何在Javascript / css中的嵌套ul中选择最低级别的ul

[英]How to select lowest level ul in nested ul in Javascript/ css

I feel like this is a simple problem and I searched quite a bit but I cannot come up with a good answer. 我觉得这是一个简单的问题,我进行了很多搜索,但找不到一个好的答案。

I have a bunch of nested ULs that I have appended to a div. 我有一堆嵌套的UL,它们已附加到div上。 I want to be able to collapse the UL I click on. 我希望能够折叠我单击的UL。 The problem is that when you click one UL inside of another UL you are actually clicking both. 问题是,当您单击另一个UL内部的一个UL时,您实际上同时单击了两个UL。 So if I click the lowest tier to collapse and say something like $(this).css( 'height', '5px') The highest level UL also gets a height of 5px. 因此,如果我单击最低层以折叠并说出$(this).css('height','5px')之类的东西,则最高级别的UL也将获得5px的高度。 Hopefully the code will make my question more clear. 希望代码可以使我的问题更加清楚。

<ul class=t0>
  <li class='item'>Top Item 1</li>
  <li class='item'>Top Item 2</li>
  <li class='item'>Top Item 3 is a list</li>
  <ul class=t1>
    <ul class=t2>
      <li class='item'>some <span class='val'>Thing</span></li>
      <li class='item'>something <span class='val'>else</span></li>
    </ul>
    <ul class=t2>
      <li class='item'>method : <span class='val'>GET</span></li>
      <li class='item'>uri : <span class='val'>/status</span></li>
    </ul>
  </ul>
<li class='item'>Top Item 3 is a list</li>
</ul>

So if you click the UL class t2 only that t2 collapses, but if you click on t1 then both the t2s collapse. 因此,如果您单击UL类t2,则只有t2折叠,但是如果您单击t1,则两个t2都折叠。 I know how to change the css and everything I am just having trouble with selecting only the ul clicked. 我知道如何更改CSS以及仅选择ul单击时遇到的所有问题。 I think I can use .find or .closest I am just not sure exactly how to do it. 我想我可以使用.find或.closest,但我不确定该怎么做。

Thanks in advance for the help. 先谢谢您的帮助。

$('.t1, .t2').click(function(e){
  e.stopPropagation(); //prevent the event from bubbling up
  $(this).slideUp();
});

This would work - however, your idea/markup/execution is flawed. 这会起作用-但是,您的想法/标记/执行存在缺陷。 There is no .t1 to click as it's made up of two .t2 's - Thus, clicking on .t1 is impossible without clicking a .t2 没有要单击的.t1 ,因为它是由两个.t2的-因此,如果不单击.t1就无法单击.t2

Your HTML is also invalid, as the <ul class='t1'> cannot have another <ul> as its child. 您的HTML也是无效的,因为<ul class='t1'>不能将另一个<ul>作为其子级。 You need some <li> 's in there. 您需要在其中一些<li>

You can use event.stopPropagation to prevent the event from going up to higher levels. 您可以使用event.stopPropagation来阻止事件进行到更高级别。 http://api.jquery.com/event.stopPropagation/ http://api.jquery.com/event.stopPropagation/

You could delegate and use e.target to determine what was clicked and find it's $.closest() ancestor UL : 您可以委派并使用e.target来确定单击的内容,然后找到它的$.closest()祖先UL

Markup (Updated) 标记(已更新)

<ul class=t0>
    <li class='item'>Top Item 1</li>
    <li class='item'>Top Item 2</li>
    <li class='item'>Top Item 3 is a list
        <ul class=t2>
            <li class='item'>some <span class='val'>Thing</span></li>
            <li class='item'>something <span class='val'>else</span></li>
        </ul>
        <ul class=t2>
            <li class='item'>method : <span class='val'>GET</span></li>
            <li class='item'>uri : <span class='val'>/status</span></li>
        </ul>
    </li>
    <li class='item'>Top Item 3 is a list</li>
</ul>

jQuery jQuery的

$('.t0').on('click', function click(e) {
    $(e.target).closest('ul').slideUp();
});

http://jsfiddle.net/userdude/ksVUY/ http://jsfiddle.net/userdude/ksVUY/

EDIT : Or is this more what you were after? 编辑 :还是是您想要的? The problem is the <ul><ul> , which is invalid markup, so it depends on what you're doing with that specific part of the markup. 问题是<ul><ul> ,它是无效的标记,因此它取决于您对标记的特定部分所做的操作。

To answer the question (which doesn't seem to be what was actually wanted but others may stumble over the question and be looking for an answer), to find the "lowest" UL you can use getElementsByTagName, which returns a collection in DOM order, and return the last element in the collection (if there is one): 要回答问题(似乎并不是真正想要的,但是其他人可能会迷惑于该问题并在寻找答案),要找到“最低” UL,可以使用getElementsByTagName,它以DOM顺序返回一个集合,并返回集合中的最后一个元素(如果有):

function getLowestUL(el) {
  var uls = el.getElementsByTagName('ul');
  return uls.length? uls[uls.length - 1]  : null;      
}

If the provided element is a UL and you want to return that if it's the lowest UL, then: 如果提供的元素是UL,并且您想返回的元素是最低的UL,则:

function getLowestUL(el) {
  var isUL = el.tagName && el.tagName.toLowerCase() == 'ul';
  var uls = (el.nodeType == 1) && el.getElementsByTagName('ul');
  return (uls && uls.length)? uls[uls.length - 1]  : isUL? el : null;      
}

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

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