JQuery ul li 单击移至列表中的下一个

发布于 2024-08-18 14:02:18 字数 566 浏览 2 评论 0原文

好的,差不多到了,希望使用 Jquery 和 css 类(图像)在 HTML ul li 列表中上下导航,就像选择选项下拉列表的工作方式一样,只想只显示 1 个项目,而不是下拉按钮,但是下一个/上一个 css 类,用于处理列表,一次只显示一个项目,因此整个效果有点像旋转器。例如,

$('.next').click (function( MOVE TO NEXT ITEM IN LIST ))
$('.prev').click (function( MOVE TO PREV ITEM IN LIST ))

请提供类似于

<ul class="">
<li> Item 1</li>
<li> Item 2</li>
<li> Item 3</li>
<li> etc ....
</ul>
<img src="but..." class="next">
<img src="but..." class="prev">

“建议”的列表 - 提前致谢

OK, nearly there, looking to use Jquery and css class (images) to navigate up and down an HTML ul li list in the same way a select option drop down works only want to only display 1 item, not a drop down button, but a next/prev css class to work though the list with only one item showing at a time so the whole effect is a bit like a spinner. e.g.

$('.next').click (function( MOVE TO NEXT ITEM IN LIST ))
$('.prev').click (function( MOVE TO PREV ITEM IN LIST ))

with a list a bit like

<ul class="">
<li> Item 1</li>
<li> Item 2</li>
<li> Item 3</li>
<li> etc ....
</ul>
<img src="but..." class="next">
<img src="but..." class="prev">

Suggestions please - and thanks in advance

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

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

发布评论

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

评论(3

Spring初心 2024-08-25 14:02:18

人们在发布代码之前需要检查他们的代码!上述答案都不起作用。

我修复它:

 var active = 0; // starts at zero
 var list = $('ul');

 list.children('li').eq('0').siblings().hide(); // Hide all except first list element

 $('.next').bind('click', function() {
  active = active == list.children('li').length-1 ? 0 : active + 1;
 });

 $('.prev').bind('click', function() {
  active = active == 0 ? list.children('li').length-1 : active - 1;
 });


 var getActive = function() {
  return list.children('li').eq(active);
 };

 $('.prev,.next').bind('click', function() {
  getActive().show().siblings().hide();
 });

People need to check their code before posting it! Neither of the above answers work.

I fixt it:

 var active = 0; // starts at zero
 var list = $('ul');

 list.children('li').eq('0').siblings().hide(); // Hide all except first list element

 $('.next').bind('click', function() {
  active = active == list.children('li').length-1 ? 0 : active + 1;
 });

 $('.prev').bind('click', function() {
  active = active == 0 ? list.children('li').length-1 : active - 1;
 });


 var getActive = function() {
  return list.children('li').eq(active);
 };

 $('.prev,.next').bind('click', function() {
  getActive().show().siblings().hide();
 });
诗化ㄋ丶相逢 2024-08-25 14:02:18
var active = 0; // starts at zero
var list = $('ul');

$('.next').bind('click', function() {
    active = active == list.length-1 ? 0 : active + 1;
});

$('.prev').bind('click', function() {
    active = active == 0 ? list.length-1 ? active - 1;
});

var getActive = function() {
    return list.children('li').eq(active);
};

现在,使用 getActive() 获取活动列表元素。

更新:如果您想隐藏除活动列表项之外的所有项目,只需添加:

$('.prev,.next').bind('click', function() {
    getActive().show().siblings().hide();
});
var active = 0; // starts at zero
var list = $('ul');

$('.next').bind('click', function() {
    active = active == list.length-1 ? 0 : active + 1;
});

$('.prev').bind('click', function() {
    active = active == 0 ? list.length-1 ? active - 1;
});

var getActive = function() {
    return list.children('li').eq(active);
};

Now, use getActive() to get the active list element.

UPDATE: if you want to hide all items except the active list item, just add:

$('.prev,.next').bind('click', function() {
    getActive().show().siblings().hide();
});
漫雪独思 2024-08-25 14:02:18

如果我正确理解你的问题,你一次只需要 1 个列表元素可见。为此,我将修改大卫答案中的函数来执行类似的操作。

var active = 0; // starts at zero
var list = $('ul');

var hideListItems = function(active) {
    $.each(list, function(count, item) {
        if (count !== active) {
            item.css({'display': 'none'});
        } else {
            item.css({'display': 'block'});
        }
    }
};

$('.next').bind('click', function() {
    active = active == list.length-1 ? 0 : active + 1;
    hideListItems(active);
});

$('.prev').bind('click', function() {
    active = active == 0 ? list.length-1 ? active - 1;
    hideListItems(active);
});

If I understand your question correctly you only want 1 list element at a time to be visible. To do this I would modify the functions from David's answer to do something like this.

var active = 0; // starts at zero
var list = $('ul');

var hideListItems = function(active) {
    $.each(list, function(count, item) {
        if (count !== active) {
            item.css({'display': 'none'});
        } else {
            item.css({'display': 'block'});
        }
    }
};

$('.next').bind('click', function() {
    active = active == list.length-1 ? 0 : active + 1;
    hideListItems(active);
});

$('.prev').bind('click', function() {
    active = active == 0 ? list.length-1 ? active - 1;
    hideListItems(active);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文