【问题标题】:javascript auto files listing in the page页面中列出的 javascript 自动文件
【发布时间】:2015-02-04 10:45:38
【问题描述】:

无论如何,例如当我导航到http://example.com/links/pages/index.html

我在 /pages 目录中有 4 个 .html 文件,名称分别为 page1.html、page2.html、page3.html、page4.html

那么 index.html 必须向我显示此 /pages 目录中存在的页面列表吗?没有我自己手动在 index.html 上列出它们。当我在 /pages 目录创建新页面时,我只希望它们在 index.html 中列出

这里有一些例子,希望对你有帮助:http://plnkr.co/edit/kIiod2DR6zgSpmuUNsUi?p=preview

或者这个例子

<!-- This is index.html page in this directory http://example.com/links/pages/index.html -->

<html>
  <head>

  </head>

  <body>

    <!-- here must be listed pages that is exist in this directory /pages -->

    <!-- example for to be listed -->
    <ul>
      <li><a href="page1.html">page 1</a></li>
      <li><a href="page2.html">page 2</a></li>
      <li><a href="page3.html">page 3</a></li>
      <li><a href="page4.html">page 4</a></li>
    </ul>

  </body>

</html>

【问题讨论】:

标签: javascript php jquery html


【解决方案1】:

这是一个例子:
在文件夹“页面”中你有:

  • page1.html
  • page2.html
  • page3.html

根目录下的index.php将是:

<html>
  <head>

  </head>

  <body>

    <!-- here must be listed pages that is exist in this directory /pages -->

    <?php
        $dir = 'pages';
        $pages = array_values(array_diff(scandir($dir), array('..', '.')));
    ?>

    <ul>
        <?php
            foreach ($pages as $page) {
                $name = str_replace('.html', '', $page);
                echo "<li><a href=\"$page\">$name</a></li>";
            }
        ?>
    </ul>

  </body>

</html>

输出将是:

<html>
  <head>

  </head>

  <body>

    <ul>
       <li><a href="page1.html">page1</a></li>
       <li><a href="page2.html">page2</a></li>
       <li><a href="page3.html">page3</a></li>
    </ul>

  </body>

</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    相关资源
    最近更新 更多