我将如何在 HTML 中将数组中的所有元素显示为可点击的对象

How would I display all elements in an array in HTML as clickable objects?

本文关键字:显示 对象 元素 HTML 数组      更新时间:2023-09-26

所以我用javascript制作了一个小脚本,如下所示:

var txtFile = new XMLHttpRequest();
txtFile.open("GET", "http://www.drakedesign.co.uk/mdmarketing/uploads/date.txt", true);
txtFile.onreadystatechange = function() {
    if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
        if( (txtFile.status == 200) || (txtFile.status == 0) ) { // Makes sure it's found the file.
            allText = txtFile.responseText;
            arrayOfLines = allText.match(/[^'r'n]+/g);
            document.getElementById("date").innerHTML =  arrayOfLines[0];
            filename1 = (arrayOfLines[0] + ".csv");
            res1 = filename1.replace("/","-");
            res2 = res1.replace("/","-");
            urlCsv = ("http://www.drakedesign.co.uk/mdmarketing/uploads/" + res2);
        }
    }
};
txtFile.send(null);

上面的代码简单解析每周更新的文本文档:http://www.drakedesign.co.uk/mdmarketing/uploads/date.txt

其中的日期是逐行写的,如下所示:

16-04-16

16-04-09

02/04/16...

在这里问我将如何转换上面的脚本来解析文本文档,然后在 html 文档中显示每个元素并使可视元素可单击,以便将我带到正确的日期。

我这样做是

不是走错了路?有没有更有效的方法呢?在这一点上,我正在考虑对所有可能出现的日期进行硬编码。但我宁愿动态地做到这一点!

提前非常感谢任何提供帮助的人。

PS:我正在使用JQUERY!对不起,之前忘了提。

你没有在你的问题中包含jQuery标签,所以我假设你正在寻找一个简单的Javascript答案。 对于您在回复中收到的每个日期,您可以将其转换为 link ,可以在您的页面上单击。

像这样:

function createLink(text, url, parentElement) {
  var a = document.createElement('a');
  var linkText = document.createTextNode(text);
  a.appendChild(linkText);
  a.href = url;
  parentElement.appendChild(a);
}

然后,您可以使用此帮助程序创建任意数量的链接:

createLink('16/04/16', 'http://www.hello.com/16/04/16', document.body);