【问题标题】:How to make the same button hide and show the block? [duplicate]如何使相同的按钮隐藏和显示块? [复制]
【发布时间】:2020-05-11 23:52:39
【问题描述】:
<button class="my_btn btn-success">Hide and Show</button></p>
<h1 id="hol">Hello World</h1>



$(".my_btn").on('click', function(event) {
    $("#hol").hide();
});

如何使相同的按钮隐藏和显示 hol 块? 请帮助,我是开始开发)

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    您可以使用 jQuery 的 .toggle()显示或隐藏匹配的元素

    $(".my_btn").on('click', function(event) {
        $("#hol").toggle();
        // you can set the specific text for the button as well
        $(this).text($(this).text() == 'Show' ? 'Hide' : 'Show');
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <button class="my_btn btn-success">Hide</button></p>
    <h1 id="hol">Hello World</h1>

    【讨论】:

      【解决方案2】:

      使用toggle在隐藏和显示之间切换

      【讨论】:

        猜你喜欢
        • 2011-08-24
        • 1970-01-01
        • 2017-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多