$(function(){ var showmorenchildren = function ($children, n) { //显示某jquery元素下的前n个隐藏的子元素 var $hiddenchildren = $children.filter(":hidden"); var cnt = $hiddenchildren.length; for ( var i = 0; i < n && i < cnt ; i++) { $hiddenchildren.eq(i).slidedown(); } return cnt-n;//返回还剩余的隐藏子元素的数量 } //对页中现有的class=showmorehandle的元素,在之后添加显示更多条,并绑定点击行为 $.showmore = function (selector) { if (selector == undefined) { selector = ".showmorenchildren" } $(selector).each(function () { var pagesize = $(this).attr("pagesize") || 10; console.log(pagesize); $(this).find("li:lt("+pagesize+")").show(); var $children = $(this).children(); if ($children.length > pagesize) { for (var i = pagesize; i < $children.length; i++) { $children.eq(i).hide(); } $("

查看更多

").insertafter($(this)).click(function () { if (showmorenchildren($children, pagesize) <= 0) { //如果目标元素已经没有隐藏的子元素了,就隐藏“点击更多的按钮条” $(this).hide(); }; }); } }); } $.showmore(".showmorenchildren"); });