简体   繁体   English

单击时更改按钮文本(更多/更少)

[英]Change button text when clicked (more/less)

Using bootstrap 3 CSS, font awesome CSS, and the latest jQuery JS files. 使用bootstrap 3 CSS,超赞的字体CSS和最新的jQuery JS文件。

I am using javascript to hide/show a div of content on top of another div of content when a button is clicked. 单击按钮时,我正在使用javascript隐藏/显示另一个内容分区上的一个内容分区。

HTML 的HTML

    <div class="col-sm-4">
        <div class="HScontainer">
        <div class="HSouter">
          <p>BOTTOM CONTENT</p>
            <div class="HSbox">
              <p>TOP CONTENT</p>
            </div>
        </div>
        <a href="#" class="HSshowit btn btn-primary" role="button">Show More <i class="fa fa-caret-square-o-up"></i></a>
    </div>
    </div>

    <div class="col-sm-4">
        <div class="HScontainer">
        <div class="HSouter">
          <p>BOTTOM CONTENT</p>
            <div class="HSbox">
              <p>TOP CONTENT</p>
            </div>
        </div>
        <a href="#" class="HSshowit btn btn-primary" role="button">Show More <i class="fa fa-caret-square-o-up"></i></a>
    </div>
    </div>

    <div class="col-sm-4">
        <div class="HScontainer">
        <div class="HSouter">
          <p>BOTTOM CONTENT</p>
            <div class="HSbox">
              <p>TOP CONTENT</p>
            </div>
        </div>
        <a href="#" class="HSshowit btn btn-primary" role="button">Show More <i class="fa fa-caret-square-o-up"></i></a>
    </div>
    </div>

</div></div></div>

CSS 的CSS

.HScontainer {
  overflow:hidden;
}

.HSouter {
  position:relative;

  width: 300px;
  height: 200px;
  background: #FBCF3C;
  border: 1px solid black;
  overflow:hidden;
}
.HSbox {
  position:absolute;
  bottom:0;
  left:0;
  width: 300px;
  height: 200px;
  margin-bottom: -200px;
  -webkit-transition: all 0.8s ease;
  -moz-transition: all 0.8s ease;
  -o-transition: all: 0.8s ease;
  transitions: all 0.8s ease;
  background: #19A4DA;
}

.HSbox p {
  margin: 0;
  padding: 5px 0 5px 0;
}

.HSshow {
    margin: 0;
}

.HSshowit { width:300px; }

JS JS

$(function(){
$('.HSshowit').click(function(e) {
    $(this).prev('.HSouter').children('.HSbox').toggleClass('HSshow');
});
});

I would like the text on the button to change from 我希望按钮上的文字从

Show More <i class="fa fa-caret-square-o-up"></i>

to

Show Less <i class="fa fa-caret-square-o-down"></i>

Here is the example... 这是例子

http://codepen.io/john84/pen/fIupa http://codepen.io/john84/pen/fIupa

This is what i usually do, Give rev attribute in the button 这就是我通常要做的,在按钮中赋予rev属性
HTML 的HTML

<a href="#" rev="more" class="HSshowit btn btn-primary" role="button">
Show More <i class="fa fa-caret-square-o-up"></i>
</a>

JS JS

$(function(){
$('.HSshowit').click(function(e) {
    $(this).prev('.HSouter').children('.HSbox').toggleClass('HSshow');

var condition = $(this).attr("rev");
  if(condition == "more"){
    $(this).html("Show Less <i class='fa fa-caret-square-o-down'>");
    $(this).attr("rev","less");
  }
  else{
    $(this).html("Show More <i class='fa fa-caret-square-o-up'>");
    $(this).attr("rev","more");
  }
e.preventDefault(); //prevent default click action from happening
});
});

Here is the result 这是结果

Set the text based on the state of the HSbox element like 根据HSbox元素的状态设置文本,例如

$(function () {
    $('.HSshowit').click(function (e) {
        var $box = $(this).prev('.HSouter').children('.HSbox').toggleClass('HSshow');
        $(this).html($box.hasClass('HSshow') ? 'Show Less <i class="fa fa-caret-square-o-down"></i>' : 'Show More <i class="fa fa-caret-square-o-up"></i>')
    });
});

Demo: Fiddle 演示: 小提琴

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

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