我有一段代码隐藏了列表中的所有奇数和偶数元素,除了前两个元素,我想隐藏其他4个元素。这是我的代码:
<body>
<ul>
<li>Milk</li>
<li>White</li>
<li>Carrots</li>
<li>Orange</li>
<li>Broccoli</li>
<li>Green</li>
</ul>
<script>
$( "li" )
.filter( ":odd" )
.hide()
.end()
.filter( ":even" )
.hover(function() {
$( this )
.toggleClass( "active" )
.next()
.stop( true, true )
.slideToggle();
});
</script>
</body>
建议我怎么做。
答案 0 :(得分:1)
使用 gt() selector 来实现它:
$( "li:gt(1)" )
.filter( ":odd" )
.hide()
.end()
.filter( ":even" )
.hover(function() {
$( this )
.toggleClass( "active" )
.next()
.stop( true, true )
.slideToggle();
});
详细了解gt() here
答案 1 :(得分:0)
试试这个。
$('.MainUl').find('li:first').hover(function() {
$('.MainUl').find('li').not($(this)).not($(this).next("li")).hide();
});
$('.MainUl').find('li:first').next("li").hover(function() {
$('.MainUl').find('li').not($(this)).not($(this).prev("li")).hide();
});