,您都会下载一个包含 url 的 txt 文件,然后我必须手动更新下拉菜单的 index.html 文件。有没有办法自动更新 url 而无需编辑索引文件?这是如何拨打文件的示例.txt每天耗尽,并且低于索引文件代码的一部分。
文本1、文本 2 和文本 3 保持不变,而 URL 每天更改
File_url.txt
<a href='http://example.com/update1/day1.txt'>text1</a><br/>
<a href='http://example.com/update2/day1.txt'>text2</a><br/>
<a href='http://example.com/update3/day1.txt'>text3</a><br/>
在索引文件中,插入 URL 是这样
文件索引.html
<!DOCTYPE html PUBLIC>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>CSS DropDown Menu</title>
<link rel="stylesheet" type="text/css" href="css/reset.css" media="screen" />
<link rel="stylesheet" type="text/css" href="css/simple.css" media="screen" />
</head>
<body>
<div id="drop-menu">
<ul id="menu">
<li><a href="http://example.com/home.php"target="principale" >Home</a></li>
<ul>
</ul>
</li>
<li><a href="#"target="principale" >Extra</a>
<ul>
<li><a href='http://example.com/update1/day1.txt'target="principale">text1</a><br/></li>
<li><a href='http://example.com/update2/day1.txt'target="principale">text2</a><br/></li>
<li><a href='http://example.com/update3/day1.txt'target="principale">text3</a><br/></li>
</ul>
</li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
<iframe name="principale" src="http://example.com/home.php" marginheight="50" height="800" width="100%" allowfullscreen = "true" ></iframe>
</body>
</html>
例如,第二天我可以找到
File_url.txt
<a href='http://example.com/update1/day2.txt'>text1</a><br/>
<a href='http://example.com/update2/day2.txt'>text2</a><br/>
<a href='http://example.com/update3/day2.txt'>text3</a><br/>
有没有办法使用永远不会更改的文本 1、文本 2 和文本 3 自动更新引用 URL?
几种方法可以解决这个问题,一种是在index.html(index.php?(中迭代file_url.txt您可以在如何在 php 中逐行读取文件中的此示例进行操作
请记住,"file_url.txt"将是文件所在位置的路径。如果你的结构是这样的,很有可能。
根目录
- 索引.php
- file_url.txt
你会想要"./file_url.txt">
...
<li><a href="#"target="principale" >Extra</a>
<ul>
<?php if ($file = fopen("./file_url.txt", "r")) {
while(!feof($file)) {
$line = fgets($file);
# do same stuff with the $line
echo sprintf(
'<li>%s</li>',
$line
);
}
fclose($file);
} ?>
</ul>
</li>
<li><a href="#">About</a></li>
...
否则,我相信javascript(jQuery(一定是要走的路。
例如,如果您使用像 PHP 这样的服务器端脚本,您可以使用像 readfile()
函数这样的函数来读取整个文件,然后使用foreach
函数迭代它的行并将项目呈现为您的 html。
但是,如果您一次不使用服务器端脚本,我认为您可以公开 txt 文件,以便可以通过 URL 访问它,例如localhost/file_url.txt
然后使用 javascript ajax 函数从 URL 获取它的内容并将它们呈现到您的 HTML 中。
以下是使用 javascript 获取文件内容的代码:
<!DOCTYPE html PUBLIC>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>CSS DropDown Menu</title>
<link rel="stylesheet" type="text/css" href="css/reset.css" media="screen" />
<link rel="stylesheet" type="text/css" href="css/simple.css" media="screen" />
</head>
<body>
<div id="drop-menu">
<ul id="menu">
<li><a href="http://example.com/home.php"target="principale" >Home</a></li>
<li><a href="#"target="principale" >Extra</a></li>
<ul>
<ul id="dynamic-menu">
</ul>
</div>
<iframe name="principale" src="http://example.com/home.php" marginheight="50" height="800" width="100%" allowfullscreen = "true" ></iframe>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
var menuRequest = $.ajax({
url: 'http://localhost:30001/file_url.txt'
});
menuRequest.done(function(data, status, xhr) {
var dynamicMenuWrapper = $('#dynamic-menu');
dynamicMenuWrapper.html(data);
});
});
</script>
</body>
</html>
*注意:请记住通过将 txt 文件指向特定 URL 来使其可访问