单击按钮显示/隐藏div

时间:2022-11-28 20:43:36

I am trying to show and hide content depending on which button is pressed. The next button should show content 2 and hide content 1, and previous button should do the opposite.

我试图显示和隐藏内容取决于按下哪个按钮。下一个按钮应该显示内容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>

6 个解决方案

#1


3  

Try toggleClass and don't forgot to use document.ready():

尝试toggleClass,不要忘记使用document.ready():

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

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

#2


3  

Use a generic class for all content

对所有内容使用泛型类

<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>

So the css would be

所以css会是

.content {
    display: none;
}

And the Javascript

和Javascript

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

#3


1  

The display property of show is none.

show的显示属性为none。

Change it to block.

将其更改为阻止。

Also, you can just use the .show() or .hide() function instead of using classes.

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

#4


1  

Try this...

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

#5


1  

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;
}

#6


0  

Have you tried transferring the show class on another line?

你试过在另一条线上转移节目类吗?

.show
{
    display: block;
}

#1


3  

Try toggleClass and don't forgot to use document.ready():

尝试toggleClass,不要忘记使用document.ready():

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

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

#2


3  

Use a generic class for all content

对所有内容使用泛型类

<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>

So the css would be

所以css会是

.content {
    display: none;
}

And the Javascript

和Javascript

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

#3


1  

The display property of show is none.

show的显示属性为none。

Change it to block.

将其更改为阻止。

Also, you can just use the .show() or .hide() function instead of using classes.

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

#4


1  

Try this...

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

#5


1  

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;
}

#6


0  

Have you tried transferring the show class on another line?

你试过在另一条线上转移节目类吗?

.show
{
    display: block;
}