简体   繁体   English

按钮不在jQuery中切换

[英]Button not toggling in jQuery

My HTML: 我的HTML:

<input type="button" id="toggleButton" value="toggle" />
<h3 id="disclaimer"> We do not guarentee the validity of the data </h3>

JavaScript/jQuery: JavaScript / jQuery:

$("#toggleButton").click( function() {
    if($("#toggleButton").is(":visible")){
        $("#disclaimer").hide();
    }else {
        $("#disclaimer").show();
    }
});

The disclaimer is supposed to be toggled between hidden and visible, whenever the button is pressed, hiding the disclaimer is just fine however once hidden it is not becoming visible again. 该免责声明应该在隐藏和可见之间切换,每当按下按钮时,隐藏免责声明就可以了,但是一旦隐藏,它就不会再次可见。

Help is needed in this regard. 在这方面需要帮助。

You could use .toggle() . 您可以使用.toggle()

$("#toggleButton").click( function() {
   $("#disclaimer").toggle();
});

The working demo. 工作演示。

Of course, as you check if the button is visible, and because the button is always visible, the disclaimers hide function is called all the time. 当然,当您检查按钮是否可见时,并且由于按钮始终可见,因此免责声明隐藏功能一直被调用。 you need to check, if disclaimer is visible or not... 您需要检查免责声明是否可见...

$("#toggleButton").click( function() {
    if($("#disclaimer").is(":visible")){
        $("#disclaimer").hide();
    }else {
        $("#disclaimer").show();
    }
});

Your javascript code should be like this: 您的JavaScript代码应如下所示:

$("#toggleButton").click( function() {
    if($("#disclaimer").is(":visible")){
        $("#disclaimer").hide();
    }else {
        $("#disclaimer").show();
    }
});

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

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