我有一个文本文件Information.txt,其中包含以下数据:
United Nations High Commissioner for Refugees,www.unhcr.org
United Nations Children's Fund,www.unicef.org
United Nations Conference on Trade and Development,www.unctad.org
United Nations Development Programme,www.undp.org
我有一个几乎什么都没做的HTML文档。
<html>
<head>
<title>Reading from text files</title>
</head>
<body>
</html>
我需要从这个HTML文档中读取Information.txt,然后打印每一行。我对这个事情完全不熟悉。我读了很多文章,但他们都谈论使用javascript或python库。有没有办法我可以读取此文件并将数据放在HTML文档的正文中。 任何单挑都将受到赞赏。
答案 0 :(得分:0)
HTML不能这样做,它只是显示文本。如果您将其设为.php文件,则以下内容将起作用:
<head>
<title>Reading from text files</title>
</head>
<body>
<?php
$f = fopen("Information.txt", "r");
// Read line by line until end of file
while(!feof($f)) {
echo fgets($f) . "<br />";
}
fclose($f);
?>
</body>
</html>
完整的教程和信用:http://html.net/tutorials/php/lesson15.php 当你使用Information.txt并且他们使用unitednations.txt和相同的数据时,我想你可能已经在那里找到了它。
正如小贴士:不要不愿意使用新语言。 HTML只是一个简单的显示器,是开始的基础。
答案 1 :(得分:-1)
尝试以下方法:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<title>Example</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(function () {
$.ajax({
url: "Information.txt",
dataType: "text",
success: function (data) {
$("#p_element").text(data);
}
});
});
</script>
</head>
<body>
<h1>Test</h1>
<p id="p_element"></p>
</body>
</html>
您必须将上述HTML文件存储在与Information.txt
相同的文件夹中。在上面的代码示例中,我将JavaScript与库jQuery一起使用。该文件是通过AJAX加载的。
一些注意事项:JavaScript在浏览器上正常运行,因此在客户端的计算机上运行。它非常强大,但缺点是不是每个客户都支持它(例如谷歌机器人,因此只有JavaScript的网站上没有任何逻辑的网站无法被谷歌索引)。您还可以通过PHP或python库在服务器端生成HTML。以上示例使用JavaScript版本。 @Frezzo用PHP发布了一个解决方案。
如果您是本主题的新手:您可以使用上面的示例作为起点。以下是jQuery使用函数的链接:
在JavaScript中寻找好的教程。