简体   繁体   English

jQuery按钮切换文本在onlick上不起作用

[英]jQuery button toggle text doesn't work onlick

I've created button to show content below. 我创建了按钮以在下面显示内容。 I need change text inside this button when this content is visible. 当此内容可见时,我需要在此按钮内更改文本。 So: 所以:

My HTML code 我的HTML代码

<button class="c-cta c-cta--show-hide">
  <div class="e-cta-text">Pokaż więcej</div>
  <span class="e-cta__icon--regular e-cta__icon--after">
    <i class="fas fa-chevron-circle-down"></i>
  </span>
</button>

and my jQuery 和我的jQuery

$(function() {
  var button = $("button.c-cta--show-hide");
  var buttonText = $(".e-cta-text");
  button.click(function() {
    $(this).next(".l-center--hidden-content").toggleClass("l-center--show-content");
    $(this).children("span.e-cta__icon--before").children("i").toggleClass("e-cta__icon--before--rotate");
    $(this).children("span.e-cta__icon--after").children("i").toggleClass("e-cta__icon--after--rotate");
  });
// function to change the text
  button.click(function() {
    if (buttonText.innerHTML === "Pokaż mniej") {
      buttonText.innerHTML = "Pokaż więcej"
    } else {
      buttonText.innerHTML = "Pokaż mniej"
    }
  })
});

How to makes it right? 如何做到正确?

Since your code mostly uses jQuery here's your text-change function completely in jQuery working: 由于您的代码主要使用jQuery,因此这完全是jQuery工作中的文本更改功能:

jsFiddle demo: https://jsfiddle.net/wfj6L38v/ jsFiddle演示: https ://jsfiddle.net/wfj6L38v/

$(function() {
    var button = $("button");
    var buttonText = $(".e-cta-text");

    button.click(function() {

       if (buttonText.text() == 'Less') {
           buttonText.html("More");
       } else {
           buttonText.html("Less");
       }

    });
});

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

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