简体   繁体   English

jQuery手风琴

[英]jQuery Accordion

Just wondering if anyone can provide some basic advice on an accordion I'm trying to simplify. 我只是想知道是否有人可以提供有关我要简化的手风琴的一些基本建议。 Got a working version, but it seems way overly complex. 有一个可用的版本,但似乎过于复杂。 Here is my new JS. 这是我的新JS。

   $(document).ready(function() {
     $("#themes li ul").hide();
     $("#themes li").hover(function() {
       $("ul").show();
         }, function() {
         $("li ul").hide();
   });

The markup looks like this: 标记如下所示:

<ul>
  <li>Tier 1
    <ul>
      <li>Tier 2</li>
      <li>Tier 2</li>
    </ul>
  </li>
  <li>Tier 1
    <ul>
      <li>Tier 2</li>
      <li>Tier 2</li>
    </ul>
   </li>
</ul>

My script works alright. 我的脚本工作正常。 But it shows all of the child ul's when any parent li is hovered, and it hides all the child ul's when unhovered. 但是,当任何父li悬停时,它会显示所有孩子ul,而当未悬停时,它会隐藏所有孩子ul。 Just not sure how I can get it to A.) Only .show the li > ul when that specific li is hovered. 只是不确定我如何将其传递给A。)仅在悬停特定的li时才显示li> ul。 And B.) Hide the shown li > ul only when another one is hovered (not itself). 和B.)仅当另一个悬停时才隐藏显示的li> ul(而不是本身)。 Example + explanation would be especially helpful! 示例+说明将特别有帮助! Thanks!! 谢谢!!

Why can't you use the JQuery UI Accordion . 为什么不能使用JQuery UI Accordion This will solve your problem. 这样可以解决您的问题。 The js is and the html is very simple here js和html非常简单

<div id="accordion">
    <h3><a href="#">First header</a></h3>
    <div>First content</div>
    <h3><a href="#">Second header</a></h3>
    <div>Second content</div>
</div>

jQuery(document).ready(function(){
    $('#accordion').accordion();
});

EDITED 已编辑

The issue with your code is it hides and displays all the 'ul' components inside any 'li' components on hover of any one li. 您的代码的问题在于,当悬停在任何一个li上时,它会隐藏并显示所有“ li”组件中的所有“ ul”组件。 Here is the code to solve this issue, this will hide/show the 'ul' which comes inside the current 'li' 这是解决此问题的代码,它将隐藏/显示当前“ li”内部的“ ul”

            $(document).ready(function() {
             $("#themes li ul").hide();
             $("#themes li").hover(function() {

               $(this).find("ul").show();
                 }, function() {
                 $(this).find("ul").hide();
           });
          });

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

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