jQuery 嵌套 ul 与 li 的父子,显示隐藏

发布于 2024-10-16 16:16:10 字数 2025 浏览 2 评论 0原文

我正在尝试创建一个简单的手风琴菜单,其中嵌套在 li 中的 ul 被隐藏,直到单击父 li,这将首先隐藏所有其他嵌套的 ul(如果暴露),然后暴露该 li 的嵌套 ul。

唯一的另一个问题是我想删除父 li 的 href。

我已经实现了大部分目标,除了我的嵌套 ul li 继承了上述操作,即使我的目标是 li 的父类。我知道我错过了一些基本的东西。提前感谢

HTML

<ul>
  <li.expanded><a href="foo">Lorum</a>
    <ul>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
    </ul>
  </li>
  <li.expanded><a href="foo">Lorum</a>
    <ul>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
    </ul>
  </li>
  <li.expanded><a href="foo">Lorum</a>
    <ul>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
    </ul>
  </li>
  <li.expanded><a href="foo">Lorum</a>
    <ul>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
    </ul>
  </li>
</ul>

jQuery

$(document).ready(function() {
    $('ul ul').hide();
    $('ul li.expanded a').removeAttr("href");
    $('ul li').click(function(){
      $('ul ul').hide('slow');
      $(this).find('ul').toggle('slow');



    });
 });
;

I'm trying to create a simple accordion menu where ul's nested in an li are hidden until the parent li is clicked which will first hide all other nested ul's if exposed and then expose the nested ul of that li.

The only other catch is that I want to remove the href for the parent li.

I've achieved most of this except my nested ul li's are inheriting the aforementioned actions even though I'm targeting the li's parent class. I know I'm missing something fundamental. Thanks in advance

HTML

<ul>
  <li.expanded><a href="foo">Lorum</a>
    <ul>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
    </ul>
  </li>
  <li.expanded><a href="foo">Lorum</a>
    <ul>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
    </ul>
  </li>
  <li.expanded><a href="foo">Lorum</a>
    <ul>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
    </ul>
  </li>
  <li.expanded><a href="foo">Lorum</a>
    <ul>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
    </ul>
  </li>
</ul>

jQuery

$(document).ready(function() {
    $('ul ul').hide();
    $('ul li.expanded a').removeAttr("href");
    $('ul li').click(function(){
      $('ul ul').hide('slow');
      $(this).find('ul').toggle('slow');



    });
 });
;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲念泪 2024-10-23 16:16:10

html 中有一些拼写错误,我已将 li.leaf 更改为 class='leaf'。我对剧本也有改动。使用 css 来隐藏所有子 ul。
查看实际效果:http://jsfiddle.net/theuideveloper/DFHWd/4/
希望这对您有帮助。
快乐编码!
-theUiDeveloper

HTML

<ul id="master">
  <li class="expanded"><a href="foo">Lorum1</a>
    <ul>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
    </ul>
  </li>
  <li class="expanded"><a href="foo">Lorum2</a>
    <ul>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
    </ul>
  </li>
  <li class="expanded"><a href="foo">Lorum3</a>
    <ul>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
    </ul>
  </li>
  <li class="expanded"><a href="foo">Lorum4</a>
    <ul>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
    </ul>
  </li>
</ul>

css

ul#master ul{
display:none;
}

jQuery

 $(document).ready(function() {
            $('ul li.expanded a').each(function(i){
            var subUl = $(this).parent().find('ul'); //Get the sub ul.
            $(this).bind('click',function(e){
                    e.preventDefault(); // Prevent the default action of the link
                    $('.expanded ul').hide(); // hide all the other ULs
                    subUl.toggle();
                }) ;   
            });
        });     

There were some typos in the html, I have the changed li.leaf to class='leaf'. I have changes in the script too. To hide all the sub ul's css is used.
See it in action : http://jsfiddle.net/theuideveloper/DFHWd/4/
Hope this helps you.
Happy coding!
-theUiDeveloper

HTML

<ul id="master">
  <li class="expanded"><a href="foo">Lorum1</a>
    <ul>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
    </ul>
  </li>
  <li class="expanded"><a href="foo">Lorum2</a>
    <ul>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
      <li.leaf><a href="foo">Lorum</a></li>
    </ul>
  </li>
  <li class="expanded"><a href="foo">Lorum3</a>
    <ul>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
    </ul>
  </li>
  <li class="expanded"><a href="foo">Lorum4</a>
    <ul>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
      <li class="leaf"><a href="foo">Lorum</a></li>
    </ul>
  </li>
</ul>

css

ul#master ul{
display:none;
}

jQuery

 $(document).ready(function() {
            $('ul li.expanded a').each(function(i){
            var subUl = $(this).parent().find('ul'); //Get the sub ul.
            $(this).bind('click',function(e){
                    e.preventDefault(); // Prevent the default action of the link
                    $('.expanded ul').hide(); // hide all the other ULs
                    subUl.toggle();
                }) ;   
            });
        });     
国粹 2024-10-23 16:16:10

您可以按如下方式更改 jQuery:

$(document).ready(function() {
    $('ul ul').hide();
    $('ul li.expanded a').removeAttr("href");
    $('ul li.expanded > a').click(function(event) {
        $('ul ul').hide('slow');
        $(this).parent().find('ul').toggle('slow');
    });
});

问题是目标 li 的子元素是 冒泡click事件,触发li.expanded的点击事件处理程序。我已更改选择器,以直接将 a 定位为 liexpanded 下的目标。

这是一个工作示例: http://jsfiddle.net/andrewwhitaker/ybVFj/

另外,您的 HTML 应该看起来像这样:

<ul>
    <li class="expanded"><a href="foo">Lorum</a>
        <ul>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
        </ul>
    </li>
    <li class="expanded"><a href="foo">Lorum</a>
        <ul>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
        </ul>
    </li>
    <li class="expanded"><a href="foo">Lorum</a>
        <ul>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
        </ul>
    </li>
    <li class="expanded"><a href="foo">Lorum</a>
        <ul>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
        </ul>
    </li>
</ul>

You could change your jQuery as follows:

$(document).ready(function() {
    $('ul ul').hide();
    $('ul li.expanded a').removeAttr("href");
    $('ul li.expanded > a').click(function(event) {
        $('ul ul').hide('slow');
        $(this).parent().find('ul').toggle('slow');
    });
});

The problem was that child elements of the targeted li were bubbling their click event up, triggering li.expanded's click event handler. I've changed the selector to target the a directly under an li with class expanded.

Here's a working example: http://jsfiddle.net/andrewwhitaker/ybVFj/

Also, your HTML should look something like this:

<ul>
    <li class="expanded"><a href="foo">Lorum</a>
        <ul>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
        </ul>
    </li>
    <li class="expanded"><a href="foo">Lorum</a>
        <ul>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
        </ul>
    </li>
    <li class="expanded"><a href="foo">Lorum</a>
        <ul>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
        </ul>
    </li>
    <li class="expanded"><a href="foo">Lorum</a>
        <ul>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
            <li class="leaf"><a href="foo">Lorum</a></li>
        </ul>
    </li>
</ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文