【问题标题】:how to create links for all files in directory with javascript? [duplicate]如何使用javascript为目录中的所有文件创建链接? [复制]
【发布时间】:2013-11-30 15:07:52
【问题描述】:

我有这样的列表,但列出了目录中的所有内容,但我想创建一个链接,如果您单击该文件,它也会读取该文件,如果它是一个目录,那么它将进入该目录..

e.g. the list is
a (folder)
b (folder)
c (folder)
d (folder)
e (folder)
f (folder)
asdf.txt
asdf.html
asdf.ini

如果我点击文件夹 a,那么如果我点击 asdf.txt,它将进入 a 目录,然后它会显示 txt 中的内容。

我有以下代码来创建所有内容,但不确定如何实现到所有目录和文件的链接

for(var I in entries)
{
    // creates li and /li
    var li = document.createElement('li');
    // creates the name of the contents
    li.appendChild(document.createTextNode(entries[I].name));
    ul.appendChild(li);
}

提前致谢。

编辑:这实际上是我尝试过的,但没有奏效,所以我将其删除并发布了我的原件......我想我也应该发布这个

    var ul = document.getElementById('contentList');
    // for all the entries inside the directory loop the following
    for(var I in entries)
    {
        // creates li and /li
        var li = document.createElement('li');
        var a = document.createElement('a');
        // creates the name of the contents

        ul.appendChild(li);
        li.appendChild(a);
        a.appendChild(document.createTextNode(entries[I].name));
        a.href = (entries[I].name);
        li.appendChild(a);
        ul.appendChild(li);

【问题讨论】:

  • 很难弄清楚如何将<a> 包含到您拥有的内容中吗?
  • DomI> 是的,我知道...我看到了那个帖子,但不知何故它不起作用
  • charliefl> 尝试起来并不难,但不知何故它不起作用,所以我喜欢@_@

标签: javascript list cordova hyperlink


【解决方案1】:

这不只是li.appendChild('<a href="…">' + entries[I].name + '</a>')); 的问题,还是创建指向文件和文件夹的不同链接的挑战?

【讨论】:

  • 是的,区分链接....我记得使用 php 并没有那么难,我相信它们在 a href="$variableOfTheFileName" 的地方工作相同,但似乎不工作 O.o
  • 那么是你试图让它们在 JavaScript 中工作的 PHP 吗?如果您发布生成文件和文件夹列表的代码以及呈现 HTML 的代码会更容易。
  • 哦,在 PHP 中,但我认为它们的工作方式可能非常相似
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-07
  • 2011-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多