jQuery Toggle Text?

后端 未结 20 956
日久生厌
日久生厌 2020-12-02 05:52

How to toggle HTML text of an anchor tag using jQuery? I want an anchor that when clicked the text alternates between Show Background & Show Text

相关标签:
20条回答
  • 2020-12-02 06:33
    $(function() {
        $("#show-background").click(function () {
            $("#content-area").animate({opacity: 'toggle'}, 'slow'); 
        });
    
        var text = $('#show-background').text();
        $('#show-background').text(
            text == "Show Background" ? "Show Text" : "Show Background");
    });
    

    Toggle hides or shows elements. You could achieve the same effect using toggle by having 2 links and toggling them when either is clicked.

    0 讨论(0)
  • 2020-12-02 06:34

    this is not the very clean and smart way but its very easy to understand and use somtimes - its like odd and even - boolean like:

      var moreOrLess = 2;
    
      $('.Btn').on('click',function(){
    
         if(moreOrLess % 2 == 0){
            $(this).text('text1');
            moreOrLess ++ ;
         }else{
            $(this).text('more'); 
            moreOrLess ++ ;
         }
    
    });
    
    0 讨论(0)
提交回复
热议问题