简体   繁体   English

显示更多按钮

[英]Show more button

I want to show LIs which have "hidden"class when I click on button. 我想在点击按钮时显示具有“隐藏”类的LI。

 $(".show-more a").on("click", function() { if(linkText === "SHOW MORE"){ linkText = "Show less"; $('.hidden').css('visible', 'visible'); $('.hidden').css('display', 'block'); } else { linkText = "Show more"; $('.hidden').css('visible', 'hidden'); $('.hidden').css('display', 'none'); }; $this.text(linkText); }); 
 .hidden { display: none; visibility: hidden; } 
 <ul> <li>Lorem</li> <li>Lorem</li> <li class="hidden">Ipsum</li> </ul> <div class="show-more"> <a href="#">Show more</a> </div> 

I think that my Javascript is absolutely wrong, I am beginner. 我认为我的Javascript绝对错误,我是初学者。 Help me Thanks 帮帮我谢谢

You are performing a strict comparison === so the letter case is important. 你正在进行严格的比较===所以信件案例很重要。

if(linkText === "Show more"){

How about removing the class that hides those elements? 删除隐藏这些元素的类怎么样?

$(".show-more a").on("click", function() {
    if(linkText === "Show more"){
        linkText = "Show less";
        $('li.hidden').removeClass('hidden')
    } else {
        linkText = "Show more";
        $('li.hidden').addClass('hidden')
    };

    $this.text(linkText);
});

jsFiddle with some additional refactors: https://jsfiddle.net/rt0cs8jh/ jsFiddle与一些额外的重构: https ://jsfiddle.net/rt0cs8jh/

You can assign a hidden class to every element you want to hide by default. 默认情况下,您可以为要hidden每个元素指定一个hidden类。 Then remove this class or just slide it on button click. 然后删除此类或只需在按钮单击时滑动它。

 var active = false; $(".show-more a").on("click", function() { $('li.hidden').slideToggle(); $(this).text(active ? 'Show more' : 'Show less') active = !active; }); 
 .hidden { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li>Lorem</li> <li>Lorem</li> <li class='hidden'>Lorem</li> <li class='hidden'>Lorem</li> <li class='hidden'>Lorem</li> <li class="hidden">Ipsum</li> </ul> <div class="show-more"> <a href="#">Show more</a> </div> 

here's how I would write it 这是我写它的方式

$(".show-more a").on("click", function() {
    var currentText = $(this).text();
    if(currentText === "Show More"){
        $('.hidden').show();
        $(this).text("Show Less");
    } else {
        $('.hidden').hide();
        $(this).text("Show More");
    };
});

You simply nee da click handler that finds all ul li elements that have the hidden class on them, then remove it. 你只需要点击处理程序找到所有具有隐藏类的ul li元素,然后将其删除。 The below code should do what youw ant. 下面的代码应该做你的蚂蚁。

$('.show-more a').on('click', function(){
  $('ul li.hidden').removeClass('hidden');
});

Using a selector like :gt(2) will make your code much more expandable. 使用类似于:gt(2)的选择器将使您的代码更加可扩展。 This selector will select all items except for the first 2 . 此选择器将选择除前2个之外的所有项目。 Additionally, checking for .is(":visible") is a slightly safer way of seeing what to set the text to. 此外,检查.is(":visible")是一种更安全的方式来查看设置文本的内容。

Additionally, using the .toggle() can be a little abrupt, if you want to use a .slideUp() and .slideDown() , you can change your code to .slideToggle() . 另外,如果要使用.slideUp().slideDown() ,使用.toggle()可能会有点突然,您可以将代码更改为.slideToggle()

 $("li:gt(2)").hide(); $(".show-more a").on("click", function() { $("li:gt(2)").toggle(); if ($("li:gt(2)").is(":visible")) { $(this).html("Show less"); } else { $(this).html("Show more"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li>Lorem</li> <li>Lorem</li> <li>Ipsum</li> <li>Ipsum</li> <li>Ipsum</li> <li>Ipsum</li> <li>Ipsum</li> <li>Ipsum</li> </ul> <div class="show-more"> <a href="#">Show more</a> </div> 

I would refactor this to add a parent element, then just toggle the state of this stuff through a parent class and change all the display level stuff in CSS. 我会重构这个以添加父元素,然后通过父类切换这些东西的状态并更改CSS中的所有显示级别的东西。

 var $parent = $('.parent'), $links = $('.show-more a'); $links.on("click", function() { $parent.toggleClass('more'); }); 
 .parent .hidden, .more li { display: none; } .more .hidden { display: list-item; } a:after { content: 'Less'; } .more a:after { content: 'More'; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="parent"> <ul> <li>Lorem</li> <li>Lorem</li> <li class="hidden">Ipsum</li> </ul> <div class="show-more"> <a href="#">Show </a> </div> </div> 

Your first and biggest problem is that you're basing your function on un existing data so it cannot perform (your function don't know what linkText is or what is linkText value. 您的第一个也是最大的问题是,您的功能基于未存在的数据,因此无法执行(您的函数不知道linkText是什么或者linkText值是什么。

Secondly, you don't have to change the css properties of the "hidden" class, you can just toggleClass it from the element. 其次,您不必更改“隐藏”类的css属性,您只需从元素中切换它。

This will do the trick: 这样就可以了:

$(".show-more a").on("click", function() {
    $("ul li").toggleClass("hidden");
    if ($(this).text() == "Show more") {
        $(this).text("Show less");
    } else {
        $(this).text("Show more");
    }
});

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

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