簡體   English   中英

添加顯示更多顯示更少按鈕

[英]Adding show more show less button

在我的網站中,為了避免復雜性,我嘗試使用jQuery添加越來越多的顯示選項。 但這不起作用。

在這里,我試圖顯示1st 10列表。

單擊顯示更多后,應顯示現有列表

 //this will execute on page load(to be more specific when document ready event occurs) if ($('.ty-compact-list').length > 10) { $('.ty-compact-list:gt(2)').hide(); $('.show-more').show(); } $('.show-more').on('click', function() { //toggle elements with class .ty-compact-list that their index is bigger than 2 $('.ty-compact-list:gt(2)').toggle(); //change text of show more element just for demonstration purposes to this demo $(this).text() === 'Show more' ? $(this).text('Show less') : $(this).text('Show more'); }); 
 .ty-compact-list { float: left; } .show-more { display: none; cursor: pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container menuitem wrapper"> <div class="filterDiv ty-compact-list soup"><img class="img1" src="images/a1.jpg"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list indo"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list soup"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list devil"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list platter"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list devil"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list starter"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list starter"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list fry"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list d&i"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list fry"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list fry"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="show-more">Show more</div> </div> 

您的代碼有一些問題。

1- $('。show-more')。css(“ display”,“ block”);

2- $(this).text($(this).text()==='顯示更多'?'顯示更少':'顯示更多');

3-移除display: none; 從CSS。

 if ($('.ty-compact-list').length > 3) { $('.ty-compact-list:gt(2)').hide(); $('.show-more').css("display", "block"); } $('.show-more').on('click', function() { //toggle elements with class .ty-compact-list that their index is bigger than 2 $('.ty-compact-list:gt(2)').toggle(); //change text of show more element just for demonstration purposes to this demo $(this).text($(this).text() === 'Show more' ? 'Show less' : 'Show more'); }); 
 .ty-compact-list { float: left; } .show-more { cursor: pointer; } .filterDiv { float: left; background-color: #ffffff; width: 450px; text-align: center; margin: 6px; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); border-radius: 20px; border: 2.5px solid #0f2f16; min-height: 300px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container menuitem wrapper"> <div class="filterDiv ty-compact-list soup"><img class="img1" src="images/a1.jpg"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list indo"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list soup"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list devil"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list platter"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list devil"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list starter"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list starter"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list fry"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list d&i"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list fry"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="filterDiv ty-compact-list fry"><img class="img1" src="images/a1.jpg" style="float: left"> <h2 class="head2">onion soup</h2> <h3 class="head3">$200</h3> <p>(Traditional soup prepared with tamarind juice, pepper corn and selected spice)</p>></div> <div class="show-more">Show more</div> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM