手风琴有多个链接

时间:2014-11-17 15:38:29

标签: javascript jquery css accordion next

我正在使用这篇CSS-Tricks文章并将其转换为UL> LI而不是DT> DD方法。我只想让粉红色的盒子在点击时显示子链接。

出于某种原因,虽然我无法让它发挥作用。我在这里创建了一个jsFiddle(单击粉红色框):



//Accordion
	(function($) {
	    
	  var allPanels = $('ul.sub-level').hide();
	    
	  $('.click-me').click(function() {
	    allPanels.slideUp();
	    alert('slide up');
        // Problem line  
	    $(this).parent().next().slideDown();
	    return false;
	  });
	
	})(jQuery);

ul { list-style: none; padding:0; margin:0; width: 400px; }
ul li { position:relative; background: #fafafa; margin-bottom:3px; height:20px; }
ul li > ul { margin-left: 30px; background: #e3e3e3; }

.click-me {  display:block; width: 20px; height: 20px; position: absolute; top:0; right:0; background: #e4f; cursor: pointer;}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
    <li><a href="#">Link somewhere</a></li>
    <li><a href="#">Link somewhere</a></li>
    <li class="test">
        <a href="http://google.com">Link somewhere</a>
        
        <!-- I want to reveal accordion using this span tag -->
        <span class="click-me"></span>
        <!-- /end -->
        
        <ul class="sub-level">
            <li><a href="#">Link 1</a></li>
            <li><a href="#">Link 2</a></li>
            <li><a href="#">Link 3</a></li>
            <li><a href="#">Link 4</a></li>
        </ul>
      <li><a href="#">Link somewhere</a></li>
    </li>
</ul>
&#13;
&#13;
&#13;

http://jsfiddle.net/ndczc728/1/

问题在于(我认为):

// Problem line  
$(this).parent().next().slideDown();

任何?

1 个答案:

答案 0 :(得分:2)

您不需要使用父母。此外,您必须从li元素中删除固定高度:

&#13;
&#13;
//Accordion
(function($) {

  var allPanels = $('ul.sub-level').hide();

  $('.click-me').click(function() {
    allPanels.slideUp();
    // Problem line  
    $(this).next().slideDown();
    return false;
  });

})(jQuery);
&#13;
ul {
  list-style: none;
  padding: 0;
  margin: 0;
  width: 400px;
}
ul li {
  position: relative;
  background: #fafafa;
  margin-bottom: 3px;
}
ul li > ul {
  margin-left: 30px;
  background: #e3e3e3;
}
.click-me {
  display: block;
  width: 20px;
  height: 20px;
  position: absolute;
  top: 0;
  right: 0;
  background: #e4f;
  cursor: pointer;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li><a href="#">Link somewhere</a>
  </li>
  <li><a href="#">Link somewhere</a>
  </li>
  <li class="test">
    <a href="http://google.com">Link somewhere</a>

    <!-- I want to reveal accordion using this span tag -->
    <span class="click-me"></span>
    <!-- /end -->

    <ul class="sub-level">
      <li><a href="#">Link 1</a>
      </li>
      <li><a href="#">Link 2</a>
      </li>
      <li><a href="#">Link 3</a>
      </li>
      <li><a href="#">Link 4</a>
      </li>
    </ul>
    <li><a href="#">Link somewhere</a>
    </li>
  </li>
</ul>
&#13;
&#13;
&#13;