根据隐藏的div更改按钮文本

时间:2014-10-10 20:26:35

标签: jquery toggle

我有两个简单的div,带有一个按钮来切换它们的可见性。我想让按钮的标题显示隐藏div的标题。所以当你拥有" Landscape" div可见,按钮会说"切换到肖像"。在展示"肖像" div,按钮应该说"切换到横向"。

<div class="container">
<div class="row">
    <div class="span12">
    <div class="panel panel-heading">
    <div class="span4"></div>
    <div class="span4">
        <button type="button" class="btn btn-default switch">16:9/9:16</button>
    </div>
    <div class="span4"></div>
    </div>
    </div>
</div>
<div class="row">
<div class="panel panel-body">
    <div class="span4 cropme" id="landscape" style="width: 1136px; height: 720px;"></div>
    <div class="span4 cropme" id="portrait" style="width: 720px; height: 1136px;"></div>
</div>
</div>

$('#portrait').hide();
$('.switch').click(function (){
  $('#portrait').toggle();
  $('#landscape').toggle();
});

http://jsfiddle.net/z2ursLfw/

2 个答案:

答案 0 :(得分:1)

$('#portrait').hide();
$('.switch').click(function () {
    $('#portrait, #landscape').toggle();
    $('button').text('Switch to ' + $('.cropme:hidden').text())
});

<强> jsFiddle example

答案 1 :(得分:1)

DEMO

<强> HTML

<button type="button" class="btn btn-default switch">Switch to Portrait</button>

<强> JS

$('#portrait').hide();
$('.switch').click(function (){
    $(this).text("Switch to "+($('#portrait').is(":visible") ? "Portrait" : "Landscape"));
  $('#portrait').toggle();
  $('#landscape').toggle();
});