html + css + jquery: Toggle Show More/Less Text

后端 未结 5 2023
离开以前
离开以前 2021-01-06 17:05

I\'m working on a personal project and I\'m having a small issue:

This is my code code and currently works: http://jsfiddle.net/gvM3b/:

$(\".show-mor         


        
5条回答
  •  孤城傲影
    2021-01-06 17:50

    Use the ternary operator, for example:

    $(".show-more").click(function () {
      var $this = $(this);
      $this.text($this.text() == "(Show Less)" ? "(Show More)" : "(Show Less)");
      $(".text").toggleClass("show-more-height");
    });​
    

    Or using .text() with a function:

    $(".show-more").click(function () {
      $(this).text(function (i, oldText) {            
        return oldText == "(Show Less)" ? "(Show More)" : "(Show Less)";      
      });
      $(".text").toggleClass("show-more-height");
    });​
    

    DEMO.

提交回复
热议问题