我需要创建一个超链接,每天都会改变,以纳入前一天的日期。
例子:
5月16日的下载链接
<a href="http://www.example.com/dir/download-2020-05-15" title="Download Link">Download</a>
5月17日的下载链接
<a href="http://www.example.com/dir/download-2020-05-16" title="Download Link">Download</a>
我知道可能会有一些脚本可以为我做这件事,但我找不到它。如果我在这里重复其他问题,我很抱歉。
我对js还很陌生,但我希望这能帮助我......
<!-- The download link in which we shall change its target URL -->
<a id="downloadLink" href="#">Download</a>
<!-- Javascript script changing the link with the id "downloadLink" target URL -->
<script type="text/javascript">
//The first part of the link without the date
var download = "http://www.example.com/dir/download-";
//Gets the link element in which the target URL will be changed
var link = document.getElementById("downloadLink");
//Gets the current date and formats it as yyyy-mm-dd
var cd = new Date().toISOString().slice(0,10);
//Finally we change the links "href" attribute so it will now include the first part of the download link and date
link.href = download + cd;
</script>
@PatMcInnes : 你可以创建3个不同元素的标签,像这样。
var elementCreatedOne = document.getElementById("a1”);
var elementCreatedTwo = document.getElementById("a2”);
var elementCreatedThree = document.getElementById("a3”);
var todaysDate = new Date();
var formattedDate= todaysDate.getFullYear() + '-' + (todaysDate.getMonth() + 1) + '-' + todaysDate.getDate();
elementCreatedOne .setAttribute("href",
“Link1” + formattedDate);
elementCreatedTwo .setAttribute("href",
“Link2” + formattedDate);
elementCreatedThree .setAttribute("href",
“Link3” + formattedDate);
HTML :
<a href="" id="a1” title="Download Link">Download</a>
<a href="" id="a2” title="Download Link">Download</a>
<a href="" id="a3” title="Download Link">Download</a>