php - 用 foreach 增加 DIV 容器

标签 php jquery html css foreach

我的标题列表中的 Java:

<script type="text/javascript">
    function showDiv(idInfo) {
        var sel = document.getElementById('divLinks').getElementsByTagName('div');
        for (var i=0; i<sel.length; i++) {
            sel[i].style.display = 'none';
        }
        document.getElementById('container'+idInfo).style.display = 'block';
    }
</script>

<style type="text/css">
    #container1, #user_elements {
        display : none;
        border : 0px solid blue;
        height : 100%;
        overflow : hidden;
    }
</style>

我不需要一堆容器,我只是用它来显示在主 DIV 中单击的新链接。

这是我的链接代码:

<div id="linkDiv">
    <?php
        $dir = "./modules";
        $folders = new DirectoryIterator($dir);
        $i=1;
        foreach ($folders as $item) {
            if ($item->isDir() && !$item->isDot()){
    ?>
    <a href="#" onclick="showDiv('<?php echo $i; ?>');return false"><?php echo $item->getFilename(); ?></a><br />
    <?php
                $i++;
            }
        }
    ?>
</div>

这是显示位置:

<div id="divLinks">
    <?php
        $path = "./modules";
        $dir = new DirectoryIterator($path);
        foreach ($dir as $fileinfo) {
            if ($fileinfo->isDir() && !$fileinfo->isDot()) {
                echo $fileinfo->getFilename().'<br>';
            }
        }
    ?>
    <!-- Increase container# by 1 for every foreach loop -->
    <div id="container1"><?php $fileinfo->getFilename() ?></div>
</div>

<!-- So if $path = Directory1 and Directory2 then the output would look like: -->
<div id="divLinks">
    <div id="container1">Container #1<p>$dir/Directory1/index.php</div>
    <div id="container2">Container #2<p>$dir/Directory2/index.php</div>
</div>

所以我需要做的是

foreach ($dir as $fileinfo) {

查找主容器或显示容器增加 1 的目录

最佳答案

你是这个意思吗?

<div id="divLinks">
    <?php
        $path = "./modules";
        $dir = new DirectoryIterator($path);
        $count= 1;
        foreach ($dir as $fileinfo) {
            if ($fileinfo->isDir() && !$fileinfo->isDot()) {
                echo '<div id="container' . $count . '">' . $fileinfo->getFilename() . '</div>';
                //increase counter by 1
                $count++;
            }
        }
    ?>
</div>

关于php - 用 foreach 增加 DIV 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26446487/

相关文章:

php - Jquery AJAX + PHP 搜索引擎添加箭头导航

PHP:西里尔(俄语)字符作为问号回显。为什么?

PHP:包含为没有输出缓冲的字符串?

jquery - 将事件绑定(bind)到 iframe 的内容并从 iframe 触发它

javascript - jquery:将其传递给子函数

html - 多分辨率布局

javascript - 尝试从 url 获取 JSON/JSONP 数据并将结果显示到 html 网页上

javascript - 我应该使用 PHP 进行数学运算,还是转换为 JavaScript?

php - 用 + 增加一个计数器并将值存储到数据库

html - 如何在 HTML 页面中使用 Google 字体