我有两个简单的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();
});
答案 0 :(得分:1)
$('#portrait').hide();
$('.switch').click(function () {
$('#portrait, #landscape').toggle();
$('button').text('Switch to ' + $('.cropme:hidden').text())
});
<强> jsFiddle example 强>
答案 1 :(得分:1)
<强> 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();
});