简体   繁体   English

如何选择子ul而不是父ul?

[英]How to select a child ul and not the parent ul?

I have a 'tree' of information that's displayed in many nested lists (ul li ul li ....). 我有一个信息的“树”,显示在许多嵌套列表中(ul li ul li ....)。 Two problems: 两个问题:

  1. When I click a <li> , the parent ul is toggled as well as the child. 当我单击<li> ,父级ul以及子级切换。
  2. If I click on an <li> in a nested list that does not have a ul in it (say, Blue under Cranberry below) the parent ul is toggle, even though this shouldn't be matching 'li.has(ul)'. 如果我点击嵌套列表中没有ul的<li> (比如下面的Cranberry下的Blue),父级ul就会切换,即使这不应该匹配'li.has(ul)' 。

I've tried various JQuery selectors, next(ul), children().first(), etc with the same results. 我已经尝试了各种JQuery选择器,next(ul),children()。first()等具有相同的结果。 No luck - though I'm sure I'm just missing something simple here. 没有运气 - 虽然我确信我只是在这里错过了一些简单的东西。

On JSFiddle . JSFiddle上

$('li.has(ul)').click(function() {
    $(this).children('ul').toggle();
});

And the html: 和HTML:

<ul class="unstyled" id="full_ontology">
    <li>Apple</li>
    <li>Banana</li>
    <li>Cranberry
        <ul>
            <li>Blue</li>
            <li>Red
                <ul>
                    <li>Round</li>
                    <li>Square</li>
                    <li>Circular</li>
                </ul>
            </li>
            <li>Purple</li>
        </ul>
    </li>
    <li>Date</li>
    <li>Elderberry
        <ul>
            <li>Yellow</li>
            <li>Pink</li>
            <li>Black</li>
        </ul>
    </li>
    <li>Fig</li>
</ul>

The problem is event propagation, prevent it 问题是事件传播,阻止它

$('li:has(ul)').click(function (e) {
    $(this).children('ul').addClass('thisOne').toggle();
});
$('li').click(function (e) {
    e.stopPropagation();
});

Demo: Fiddle 演示: 小提琴

It's all about propagation. 这都是关于传播的。

Here is the working JavaScript: 这是有效的JavaScript:

$('li').click(function(e) {
    if ($(this).has('ul')) {
        $(this).children('ul').addClass('thisOne').toggle();
    }

    e.stopPropagation();
});

JSFiddle: http://jsfiddle.net/zkFGU/3/ JSFiddle: http//jsfiddle.net/zkFGU/3/

Basically, first you want this event on all of the list items, not just the ones that have the ul. 基本上,首先,您希望在所有列表项上使用此事件,而不仅仅是具有ul的列表项。 The reason is because if you don't, you can't stop propagation. 原因是因为如果你不这样做,你就无法阻止传播。 For example, "Blue" under "Cranberry" has no list, so it wouldn't call the event. 例如,“蔓越莓”下的“蓝色”没有列表,因此它不会调用该事件。 However, because "Blue" is actually in Cranberry, which is a list item and does trigger the event, clicking Blue counts as clicking Cranberry, so Cranberry retracts. 但是,因为“Blue”实际上是Cranberry,它是一个列表项并且确实触发了该事件,所以单击Blue计算为单击Cranberry,因此Cranberry缩回。 By giving Blue a change to chance to stop propagation, it prevents Cranberry from erroneously collapsing. 通过给予蓝色改变机会来阻止传播,它可以防止蔓越莓错误地崩溃。

The other part of stopping propagation is simply to stop parents. 阻止传播的另一部分就是阻止父母。 We want only the element we directly clicked on (which is triggered first) and no other element to (attempt) to toggle. 我们只想要直接点击的元素(首先被触发)而没有其他元素(尝试)切换。

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

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