在超链接过程中使用带有javascript功能的django标记

using django tags with javascript function during hyperlinking

本文关键字:功能 javascript django 标记 超链接 过程中      更新时间:2023-09-26

我有一个html代码片段

<a href = "{% url home %}?p=1">home</a>

我想在url的末尾添加一个对javascript函数的调用,如果主页url为http://localhost/home则上述代码生成

http://localhost/home?p=1

但是我不知道如何在它的末尾添加一个参数。下列不起作用

<a href = "{% url home %}?p=1"+getParameters()>home</a>

您没有正确使用模板标记(请参阅:https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#url)

正确:

{% url <url-name> %}

现在"{{url-home}}"返回空字符串,所以您正在为当前url添加GET参数。

{{x}}表单返回名为"x"的变量。

使用JS向URL添加参数

这是点击脚本更改锚点url:的简单示例

<a href="{% url home %}?p=1" onclick="return getParameters(this);">anchor</a>
<script>
function getParameters(elem) {
    elem.href = elem.href + "&abc=1";
}
</script>