3

在我搜索这个主题的过程中,我想得到一些帮助。

在我的网页中,我想建立一个 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***

拜托,能给我一点建议吗?

啤酒。

4

1 回答 1

12

HTML

<a href="#" id="myUniqueLinkId">name of link</a>

JS

var month = (new Date()).getMonth();
var myURL = 'http://domain.tld/myLocalFile.php?month=' + month + '&param=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 中而是在您的服务器端代码中处理这个问题,并在那里生成正确的链接。

于 2013-09-03T08:42:06.977 回答