$(function () { $('.p_level3Item').each(function () { var leve3length = $(this).parent().children().length; if (leve3length <= 3) { $(this).parent().addClass('no_leve3up'); } console.log(leve3length) }); $('.show-more').click(function () { var isclasname = $(this).prev().find('li').is('.leve3list'); if (isclasname) { $(this).prev().find('li').show(); $(this).prev().find('li').removeClass('leve3list'); } else { $(this).prev().find('li').removeAttr('style'); $(this).prev().find('li').addClass('leve3list'); } }); });