jquery垂直导航-slideUp slipDown
我使用这个html结构:
<div id="products">
<ul>
<li class="selected"><a href="#">Item 1</a>
<ul>
<li><a href="#">Sub-Item 1 a</a></li>
<li><a href="#">Sub-Item 1 b</a></li>
<li><a href="#">Sub-Item 1 c</a></li>
</ul>
</li>
<li><a href="#">Item 2</a>
<ul>
<li><a href="#">Sub-Item 2 a</a></li>
<li><a href="#">Sub-Item 2 b</a></li>
</ul>
</li>
<li><a href="#">Item 3</a>
<ul>
<li><a href="#">Sub-Item 3 a</a></li>
<li><a href="#">Sub-Item 3 b</a></li>
<li><a href="#">Sub-Item 3 c</a></li>
<li><a href="#">Sub-Item 3 d</a></li>
</ul>
</li>
<li><a href="#">Item 4</a>
<ul>
<li><a href="#">Sub-Item 4 a</a></li>
<li><a href="#">Sub-Item 4 b</a></li>
<li><a href="#">Sub-Item 4 c</a></li>
</ul>
</li>
</ul>
</div>
和这个jquery代码:
$(document).ready(function () {
$("#products li").click(function() {
if ($(this).attr('class') != 'selected') {
$('#products li').removeClass('selected');
$(this).addClass('selected');
}
});
$("#products li").click(function() {
$('#products li ul').slideUp();
$('#products li.selected ul').slideDown();
});
});
问题:
我只想对第一级的项目有上下滑动效果(
还有另一个问题 - 如果列表项已经是 class="selected" 并且您单击它,它会向上滑动,然后再次向下滑动..(如果已经是,则不会发生任何情况)
有人可以帮助使用 jquery代码?
i use this html structure:
<div id="products">
<ul>
<li class="selected"><a href="#">Item 1</a>
<ul>
<li><a href="#">Sub-Item 1 a</a></li>
<li><a href="#">Sub-Item 1 b</a></li>
<li><a href="#">Sub-Item 1 c</a></li>
</ul>
</li>
<li><a href="#">Item 2</a>
<ul>
<li><a href="#">Sub-Item 2 a</a></li>
<li><a href="#">Sub-Item 2 b</a></li>
</ul>
</li>
<li><a href="#">Item 3</a>
<ul>
<li><a href="#">Sub-Item 3 a</a></li>
<li><a href="#">Sub-Item 3 b</a></li>
<li><a href="#">Sub-Item 3 c</a></li>
<li><a href="#">Sub-Item 3 d</a></li>
</ul>
</li>
<li><a href="#">Item 4</a>
<ul>
<li><a href="#">Sub-Item 4 a</a></li>
<li><a href="#">Sub-Item 4 b</a></li>
<li><a href="#">Sub-Item 4 c</a></li>
</ul>
</li>
</ul>
</div>
and this jquery code:
$(document).ready(function () {
$("#products li").click(function() {
if ($(this).attr('class') != 'selected') {
$('#products li').removeClass('selected');
$(this).addClass('selected');
}
});
$("#products li").click(function() {
$('#products li ul').slideUp();
$('#products li.selected ul').slideDown();
});
});
Problem:
i only want to have the slide up and down effect on items of the first level (< li >)
not on the sub-items
an other problem - if the list item is already class="selected" and you click on it it slides up and then again down.. (nothing should happen if it is already )
can someone help with the jquery code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
演示: http://jsfiddle.net/LEdgT/6/< /a>
Try this out:
Demo: http://jsfiddle.net/LEdgT/6/
将选择器更改为
$('#products > ul > li')
,它将仅选择直接子级。或者您可以将整个代码更改为更具可读性且更少重复的代码:
Change your selector to
$('#products > ul > li')
, it would select only the immediate children.Or you could change the entire code to something more readable and less repetetive:
您可以使用
:not
选择器或.not
来完成此操作You can do it with
:not
selector or.not