在我搜索这个主题的过程中,我想得到一些帮助。
在我的网页中,我想建立一个 HREF 链接,根据当前月份重定向到一个精确的网页。
我的链接是:
<td><a href="/comptes/mon_compte.html?display=affilies_periode&id=${vendeur.id}& month=javascript:monthNumber">Détails pour le mois en cours</a></td>
还有我在 JS 中的代码:
<script language="JavaScript">
date1 = new Date();
document.write("<p>date1</p>");
monthNumber = date1.getMonth();
document.write("<p>monthNumber</p>");
</script>
根据月的结果,我想使查询像这样动态化:
http://localhost:8080/comptes/mon_compte.html?display=affilies_periode&id=2&***month=javascript:monthNumber***
拜托,你能给我一些建议吗?
麦芽酒。
最佳答案
HTML
<a href="#" id="myUniqueLinkId">name of link</a>
JS
var month = (new Date()).getMonth();
var myURL = 'http://domain.tld/myLocalFile.php?month=' + month + '¶m=1';
document.getElementById('myUniqueLinkId').href = myURL;
或者只是在运行时完全处理它:
<a onclick="window.location='http://domain.tld/myLocalFile.php?month='
+ (new Date()).getMonth();return false;" href="#">name of link</a>
最好的解决方案仍然是不在 JS 中而是在服务器端代码中处理此问题,并在那里生成正确的链接。
关于javascript - 与javascript的动态链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18587633/