php - 在 PHP 中动态显示/隐藏 Div

标签 php javascript show-hide

我有一个动态生成 Div 的数组。现在我想隐藏并显示该 div,并且应该一次显示 1 个 div,并且要显示下一个 div,用户必须单击按钮。如下所示:

<?php $h=0;?>
<script stype="textjavascript">
        function test() {
          document.getElementById("set").style.display="none";

          document.getElementById("set<?php echo $h+1; ?>").style.display="block";

          }
</script>
<?php
foreach($sets as $set){ 
if($h==0)
{
?>
<div id="set">
</php } else { ?>
<div id="set<?php echo $h;?>" style="display:none;">

<p><a class="continue" href="#" onclick="test()">Continue</a></p>
</div>
<?php $h++; } } ?>

现在使用上面的代码我可以隐藏和显示 2 div,但在 3 div 的情况下它不起作用,请让我知道我哪里做错了。

最佳答案

试试这个代码。

<?php
$sets = array('one','two','three','four');
?>
<script stype="textjavascript">
    var current = 0;
    var total = <?php echo count($sets); ?>;
    function test() {
        for(var i=0;i<total;i++)
        {
            document.getElementById("set"+i).style.display="none";
        }
        current++;
        document.getElementById("set"+current).style.display="block";
    }
</script>
<?php
foreach($sets as $key=>$set){ 
?>
<div <? if($key>0){ ?> style="display: none" <? } ?> id="set<?php echo $key; ?>">
    == <?php echo $set; ?> ==
<p><a class="continue" href="#" onclick="test()">Continue</a></p>
</div>
<?php  } ?>

关于php - 在 PHP 中动态显示/隐藏 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13470977/

相关文章:

php - php 表单提交按钮不起作用

php - 如何正确连接两个表的数据?

php - 当对短网址使用唯一的字母数字字符串时,将创建的字符串存储在数据库中还是即时编码/解码更好?

php - 连接到/var/www 之外的 SQLite 数据库

javascript - 组件模板应该只包含一个根元素

ionic-framework - 当 Ionic 3 中的输入处于事件状态时,显示/隐藏密码按钮不起作用

javascript - 数据未从子组件刷新到父组件

javascript - fps 保存在 html5 视频的元数据中的什么位置?

jquery - 如何用按钮隐藏和显示div?显示div时如何给按钮添加效果?