简体   繁体   English

使用jQuery在两个按钮之间切换

[英]Toggling between two buttons using jQuery

I've seen a few articles about this dotted around but I cant seem to get their solutions to work for me. 我看过一些有关此问题的文章,但我似乎无法获得他们的解决方案为我工作。

What I have are two buttons which control the show() and hide() states of different div's. 我有两个按钮来控制不同div的show()和hide()状态。 On page load both of the div's are set to .hide() as the user doesn't need to see them until clicked. 在页面加载时,两个div都设置为.hide(),因为用户在单击之前无需查看它们。

So, I have two buttons a and b which currently work perfectly however you can show() both div's at the same time which I don't want to happen. 因此,我有两个按钮a和b目前可以正常使用,但是您可以同时显示两个不想出现的div。 The current code resembles 当前代码类似于

$('#a-div).hide();
$('#b-div).hide();
$('#a').click(function(){
    $('#a-div).toggle(500);
});
$('#b').click(function(){
    $('#b-div).toggle(500);
});

So how can I re-write this so that if #a-div is visible (already tried the .is(':visible') method) and #b is clicked nothing happens until #a-div is hidden again and vis versa? 那么,如何重新编写该代码,以便如果#a-div是可见的(已经尝试过.is(':visible')方法)并且单击了#b,直到#a-div被再次隐藏,反之亦然?

probably you need to apply concept like this 

$('#a-div).hide();
$('#b-div).hide();
$('#a').click(function(){
if ($('#b').isVisible)[you can check via css property as well]
{
    $('#b-div).toggle(500); [or set css property visiblity:hidden]
    $('#a-div).toggle(500);
}
else {$('#a-div).toggle(500);}
});
$('#b').click(function(){
if ($('#a').isVisible)[you can check via css property as well]
{
    $('#a-div).toggle(500); [or set css property visiblity:hidden]
    $('#b-div).toggle(500);
}
else {$('#b-div).toggle(500);}
});

Try this 尝试这个

$('#a-div').hide();
$('#b-div').hide();
$('#a').click(function(){
    $('#a-div').toggle(500);
    if($('#b-div').is(":visible"))
        $('#b-div').hide();

});
$('#b').click(function(){
    $('#b-div').toggle(500);
    if($('#a-div').is(":visible"))
        $('#a-div').hide();
});

What I ended up doing is this 我最终要做的是这个

$('#a-div').hide();
$('#b-div').hide();

$('#a').click(function(){
    $('#a-div').toggle();
    $('#b-div').hide();
});
$('#b').click(function(){
    $('#b-div').toggle();
    $('#a-div').hide();
});

For anyone who is interested. 对于任何有兴趣的人。 Prior to this I was making this much more complex than it needed to be. 在此之前,我使事情变得复杂得多。

Another solution is to create a universal function and pass the parameters of the shown and hidden objects. 另一个解决方案是创建一个通用函数并传递显示对象和隐藏对象的参数。 This way you can use the same method for future elements: 这样,您可以对以后的元素使用相同的方法:

function toggleDivs($show, $hide) {
    $show.toggle();
    $hide.hide();
}
$("#b").on("click", function() { toggleDivs($("#b-div"), $("#a-div")); });
$("#a").on("click", function() { toggleDivs($("#a-div"), $("#b-div")); });

The only item missing is to initially hide the div objects, but I would add a css class to the objects to hide them. 唯一缺少的一项是最初隐藏div对象,但是我将向对象添加一个CSS类以隐藏它们。

HTML 的HTML

<button id="a">Show A</button>
<button id="b">Show B</button>

<div id="a-div" class="hideDiv">A</div>
<div id="b-div" class="hideDiv">B</div>

CSS 的CSS

.hideDiv { display:none; }

 var $aDiv = $('#a-div'); var $bDiv = $('#b-div'); var $aBtn = $('#a'); var $bBtn = $('#b'); $aDiv.hide(); $bDiv.hide(); $aBtn.click(function(){ $aDiv.toggle(500, function(){ if($aDiv.is(":visible")) $bBtn.prop("disabled",true); else $bBtn.prop("disabled",false); }); }); $bBtn.click(function(){ $bDiv.toggle(500, function(){ if($bDiv.is(":visible")) $aBtn.prop("disabled",true); else $aBtn.prop("disabled",false); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="a-div">div a</div> <div id="b-div">div b</div> <button id="a">btn a</button> <button id="b">btn b</button> 

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

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