限制ul元素中显示的项目
Limit the items shown in a ul element
以下是我的代码,用于限制ul
:中可见列表项的数量
$wnd.$(el)
.find('li:gt(9)')
.hide()
.end()
.append(
$wnd.$('<li>+More</li>').click( function(){
$wnd.$(this).siblings(':hidden').show().end().remove();
})
);
单击+More
时,它将立即显示所有项目。
应该在代码中添加什么,这样当单击+More
时,它还不会显示所有内容,只显示接下来的10项?然后,当显示所有项目时,应该显示一个+Less
选项,只显示前10个项目,就像以前一样。
这是一个奇怪的语法,但像这样的东西可能是
$wnd.$(el)
.find('li:gt(9)')
.hide()
.end()
.append(
$wnd.$('<li>+More</li>').click(function () {
$wnd.$(this).remove().siblings(':hidden').slice(0, 10).show();
if ($wnd.$(el).find('li:hidden').length === 0) {
$wnd.$(el).append(
$wnd.$('<li>+Less</li>').click(function () {
$wnd.$(el).find('li:gt(9)').hide();
})
)
}
})
);
这是一种方法:
$( 'li:gt(9)' )
.hide()
.parent()
.append(
$( '<li>more</li>' ).on( 'click', function() {
var el = $( this ).siblings( ':hidden' );
if ( el.length ) el.slice( 0, 10 ).show();
else $( 'li:gt(9)' ).not( this ).hide();
$( this ).text( $( this ).siblings( ':hidden' ).length ? 'more' : 'less' );
})
);
FIDDLE
这个怎么样?https://jsfiddle.net/0hyL9ngr/
HTML
<ul id='some-list'>
<li class='list-item'>1</li>
<li class='list-item'>2</li>
<li class='list-item'>3</li>
...
<li class='list-item'>39</li>
<li class='list-item'>40</li>
<li class='list-item'>41</li>
<li id='show-more'>More</li>
<li id='show-first-ten'>Less</li>
</ul>
JavaScript
$('#show-more').on('click', function () {
$('.list-item:hidden:lt(10)').show();
if ($('.list-item:hidden').length == 0) {
$(this).hide();
$('#show-first-ten').show();
}
});
$('#show-first-ten').on('click', function () {
$('.list-item:gt(9)').hide();
$(this).hide();
$('#show-more').show();
});
CSS
#show-more {
cursor: pointer;
}
#show-first-ten {
display: none;
cursor: pointer;
}
.list-item:nth-child(n+11) {
display: none;
}
相关文章:
- 角度的项目列表 ng 重复,ng 单击显示全宽描述
- 我怎样才能重复显示接下来的15个项目
- 隐藏/显示包含单词的ul li项目.但只能入住李的子女
- Javascript中的列表,用于添加和显示所有项目
- 如何在点击时显示ul项目
- 限制ul元素中显示的项目
- 在underscore.js中显示列表中的所有项目(使用Parse.com)
- PHP无法显示带有“”的项目'"在他们身上
- HTML选择,在DOM中选择了正确的选项,但在firefox中显示了错误的项目
- 获取中继器控件ASP.Net中显示的单击项目文本框
- 仅显示具有userid作为子项的项目
- 从项目列表Jquery中仅选择(显示:块)元素
- 首先显示选择最多的项目
- 如果项目不匹配,则显示变量
- 只有一个项目显示在 x 轴 d3 中 - 使用刻度值的序号
- 在 HTML 中将数组的每个项目显示为换行符
- 如何正确使用熨斗可选?像铁页一样使用时,项目显示为null
- Html, javascript:滚动列表框项目-并为每个项目显示提示
- 根据从下拉列表中选择的项目显示ViewBag的属性并发布ID
- 如何向选中的项目显示一个箭头?