按钮点击显示/隐藏DIV(Show/hide div on button click)

2019-07-19 03:18发布

我试图显示和隐藏的内容取决于哪个按钮被按下。 下一步按钮应该显示的内容2和隐藏内容1,和以前的按钮应该做的正好相反。

<script type="text/javascript">
    $('a.button-next').click(function() {
        $("#tab-content2").addClass("show");
    });
</script>

CSS:

#tab-content2 {
    display: none;
}
#tab-content2.show {
    display: none;
}

HTML:

<div id="tab-content1">             
    <?php the_content(); ?>
</div>

<div id="tab-content2">     
    <?php the_field("experience");?>
</div>

<a href="javascript:;" class="button-back">Previous</a>
<a href="javascript:;" class="button-next">next</a>

Answer 1:

尝试toggleClass不要忘了使用的document.ready() :

$(document).ready(function() {
    $('a.button-next').click(function() {
        $("#tab-content2").toggleClass("show");
    });
});

#tab-content2.show {display:block;}


Answer 2:

使用泛型类的所有内容

<div class="content" id="tab-content1">             
    <?php the_content(); ?>
</div>
<div class="content" id="tab-content2">     
    <?php the_field("experience");?>
</div>

<a href="javascript:;" class="button-back">Previous</a>
<a href="javascript:;" class="button-next">next</a>

所以CSS会

.content {
    display: none;
}

和JavaScript

$('a.button-next').click(function() {
    $('.content').hide(); // To hide all other contents
    $("#tab-content2").show(); // Show the one content you want to display
});


Answer 3:

的显示属性shownone

将其更改为block

此外,您还可以只使用.show().hide()函数,而不是使用类。



Answer 4:

试试这个...

$('a.button-next').on('click', function() {
    $("#tab-content2").toggle("show");
});


Answer 5:

HTML

<div id="tab-content-holder">
    <div id="tab-content1 show">             
        <?php the_content(); ?>
    </div>

    <div id="tab-content2">     
        <?php the_field("experience");?>
    </div>
</div>

<a href="#" class="button-back">Previous</a>
<a href="#" class="button-next">Next</a>

JS

$(document).ready(function() {
    $(".button-back").click(function() {
        MenuNavigationClick(-1);
    });
    $(".button-next").click(function() {
        MenuNavigationClick(1);
    });

    function MenuNavigationClick(direction) {
        // Get current element index and toggle
        var current = $("#tab-content-holder .show");
        var index = current.index();
        current.toggleClass("show");

        // Move to next element and check for overflow
        index += 1 * direction;
        index %= $("#tab-content-holder div").length;

        // Toggle next element
        $("#tab-content-holder div:eq("+index+")").toggleClass("show");
    }
});

CSS

#tab-content-holder div {
    display: none;
}
#tab-content-holder div.show {
    display: block;
}


Answer 6:

您是否尝试另一条线路上传输的节目类?

.show
{
    display: block;
}


文章来源: Show/hide div on button click