首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HTML:"onclick“执行每次刷新操作

HTML:"onclick“执行每次刷新操作
EN

Stack Overflow用户
提问于 2021-06-09 11:13:08
回答 1查看 47关注 0票数 0

我不知道为什么每次刷新都要执行"onclick“操作(按一下按钮或我都试过了)。打开此页面时,可以观察到与"set_active_experiment()“和"create_experiment()”相关的操作。我只想在单击链接/按钮时调用这些函数。你有什么想法吗?

代码:

代码语言:javascript
复制
<html>
<head>
    <title>Experiments</title>
</head>

<body>
    <div class="experiments">
        <h3>Your experiments:</h3>
        {% with experiments = get_user_experiments() %}
            {% if experiments %}
                    {% for exp in experiments %}
                        <a href="{{ url_for('experiment') }}"><button onclick="{{ set_active_experiment(exp) }}">{{ exp }}</button></a>
                    {% endfor %}
            {% endif %}
        {% endwith %}
    </div>
    <div>
        <a href="{{ url_for('experiments') }}"><button onclick="{{ create_experiment() }}">Create new experiment</button></a>
    </div>
</body>
EN

回答 1

Stack Overflow用户

发布于 2021-06-09 11:26:49

您将立即调用这些函数,因为您已经将它们封装在了花括号中。这意味着,代码将被计算为并立即执行。

代码语言:javascript
复制
<button onclick="{{ create_experiment() }}">Create new experiment</button>

如果要调用单击时的函数,请删除括号,并将调用添加为字符串文本

代码语言:javascript
复制
<button onclick="create_experiment()">Create new experiment</button>

在调用set_active_experiment()时也是如此。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67903045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档