简体   繁体   English

jQuery切换隐藏和切换显示

[英]jquery toggle hide and toggle show

I am trying to Load first 10 element.and remaining element show after click more . 我正在尝试加载前10个element.click更多后显示其余元素。 I am using stack overflow solution.you can see here . 我正在使用堆栈溢出解决方案。您可以在这里看到。 jQuery load first 3 elements, click "load more" to display next 5 elements . jQuery加载前3个元素,单击“加载更多”以显示后5个元素 Now I am trying to do some changes. 现在,我正在尝试进行一些更改。 1:I want show 10 element then view more tag. 1:我想显示10个元素,然后查看更多标签。 with tootle hide and show. 与草龟捉迷藏。 2:when load more button click then show all li element and after all li element loaded, Load more button changes as show less button and load more button hide.and when click show less. 2:当点击更多按钮后显示所有li元素,当全部li元素被加载后,点击更多按钮将显示更多按钮,隐藏更多按钮。 show less will be hide and load more button will be show. 显示较少将被隐藏并显示更多加载按钮。

<ul id="myList">
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
    <li>Four</li>
    <li>Five</li>
    <li>Six</li>
    <li>Seven</li>
    <li>Eight</li>
    <li>Nine</li>
    <li>Ten</li>
    <li>Eleven</li>
    <li>Twelve</li>
    <li>Thirteen</li>
    <li>Fourteen</li>
    <li>Fifteen</li>
    <li>Sixteen</li>
    <li>Seventeen</li>
    <li>Eighteen</li>
    <li>Nineteen</li>
    <li>Twenty one</li>
    <li>Twenty two</li>
    <li>Twenty three</li>
    <li>Twenty four</li>
    <li>Twenty five</li>
</ul>
<div id="loadMore">Load more</div>
<div id="showLess">Show less</div>

jquery jQuery的

$(document).ready(function () {
    size_li = $("#myList li").size();
    x=3;
    $('#myList li:lt('+x+')').show();
    $('#loadMore').click(function () {
        x= (x+5 <= size_li) ? x+5 : size_li;
        $('#myList li:lt('+x+')').show();
    });
    $('#showLess').click(function () {
        x=(x-5<0) ? 3 : x-5;
        $('#myList li').not(':lt('+x+')').hide();
    });
});

css 的CSS

#myList li{ display:none;
}
#loadMore {
    color:green;
    cursor:pointer;
}
#loadMore:hover {
    color:black;
}
#showLess {
    color:red;
    cursor:pointer;
}
#showLess:hover {
    color:black;
}

same as below ^O^ : 与下面的^ O ^相同:

 $(document).ready(function () { size_li = $("#myList li").size(); x=10; $('#myList li:lt('+x+')').show(); if (size_li > 10) { $('#loadMore').show(); } $('#loadMore').click(function () { $('#myList li, #showLess').show(); $(this).hide(); }); $('#showLess').click(function () { $('#showLess, #myList li').hide(); $('#loadMore, #myList li:lt('+x+')').show(); }); }); 
 #myList li{ display:none; } #loadMore { display: none; color:green; cursor:pointer; } #loadMore:hover { color:black; } #showLess { display: none; color:red; cursor:pointer; } #showLess:hover { color:black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul id="myList"> <li>One</li> <li>Two</li> <li>Three</li> <li>Four</li> <li>Five</li> <li>Six</li> <li>Seven</li> <li>Eight</li> <li>Nine</li> <li>Ten</li> <li>Eleven</li> <li>Twelve</li> <li>Thirteen</li> <li>Fourteen</li> <li>Fifteen</li> <li>Sixteen</li> <li>Seventeen</li> <li>Eighteen</li> <li>Nineteen</li> <li>Twenty one</li> <li>Twenty two</li> <li>Twenty three</li> <li>Twenty four</li> <li>Twenty five</li> </ul> <div id="loadMore">Load more</div> <div id="showLess">Show less</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM