繁体   English   中英

点击切换文本的Jquery按钮和另一个元素的css类

[英]Jquery button on click toggle text and css class of another element

我在click事件上创建一个jquery来切换div#foo的css类(在现有类之后附加它),但是还需要在show / hide之间切换按钮的文本。

<script>
$(document).ready(function(){
  $("button.toggler").click(function(){
    $("#foo").toggleClass("maximize");
    $("button.CsToggle").text(!text == "Expand" ? "Hide" : "Expand");
  });
});
</script>

<div id="foo" class="preExistingClass">
  <div>
    <button class="toggler">Expand</button>
  </div>
</div>

我让班级切换工作正常但不是文本切换。 我哪里做错了?

使用html()函数设置按钮的innerHtml。 选择器也应该是$("button.toggler") 你还需要一个可变的文本,我假设你的业务逻辑将决定它是如何设置的..

$(document).ready(function(){
  $("button.toggler").click(function(){
    $("#foo").toggleClass("maximize");
    $(this).html($(this).html() == "Expand" ? "Hide" : "Expand");
  });
});

工作示例: http//jsfiddle.net/z5QVM/

$("button.CsToggle").toggle(function(){
 if ( $(this).text == "Expand"){ 
     // code
     }
  else {
     //code
   }
});

暂无
暂无

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

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