gpt4 book ai didi

JQuery ul li 选择列表

转载 作者:行者123 更新时间:2023-12-03 22:59:23 25 4
gpt4 key购买 nike

尝试使用 JQuery 使用类 next 和类 prev 滚动浏览 ul li 列表,例如

<ul class="selectoption">
<li> Item 1</li>
<li> Item 2</li>
<li> Item 3</li>
<li> ... </li>
</ul>
<a href="" class="next">Next</a>
<a href="" class="prev">Back</a>

唯一的事情是我只希望所选的 li 可见。那么需要以某种方式索引 li 吗?非常感谢您的帮助 - 提前致谢

最佳答案

这应该可以做到:

// Hide all but the first
$('.selectoption li').not(':first').hide();

// Handle the click of prev and next links
$('.prev, .next').click(function() {
// Determine the direction, -1 is prev, 1 is next
var dir = $(this).hasClass('prev') ? -1 : 1;
// Get the li that is currently visible
var current = $('.selectoption li:visible');

// Get the element that should be shown next according to direction
var new_el = dir < 0 ? current.prev('li') : current.next('li');

// If we've reached the end, select first/last depending on direction
if(new_el.size() == 0) {
new_el = $('.selectoption li:'+(dir < 0 ? 'last' : 'first'));
}

// Hide them all..
$('.selectoption li').hide();
// And show the new one
new_el.show();

// Prevent the link from actually redirecting the browser somewhere
return false;
});

关于JQuery ul li 选择列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2071189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com