Delete 最佳答案 您在这里对客户端和服务器端感到困惑,ej-6ren">
gpt4 book ai didi

javascript - 按钮 Onclick 函数在加载时执行

转载 作者:行者123 更新时间:2023-11-30 06:15:14 24 4
gpt4 key购买 nike

当页面加载时,无论按钮是否被点击,该函数都会执行,请注意我使用的是 EJS。

我尝试更改 <%标记为 <%=<%- ,并使其成为箭头函数:<% () => Delt(this.id);%> .

<%function Delt() { Array.pop(); }%>
<button onclick="<%Delt(this.id);%>">Delete</button>

最佳答案

您在这里对客户端和服务器端感到困惑,ejs 标记中的任何内容都会在您的 dom 加载之前在服务器上执行。因此 ejs 标记中定义的函数被执行,因为它们不在客户端 js 中。尝试在客户端 js 而不是 ejs 中定义您的函数,函数调用也是如此。

<%function Delt() { Array.pop(); }%> // function definition in ejs
<button onclick="<%Delt(this.id);%>">Delete</button> // function call in ejs

So all the stuff gets executed in server before your dom loads,you are just making normal function call in ejs ,it doesn't sees your onclick coz that's your browser's part.

关于javascript - 按钮 Onclick 函数在加载时执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56567229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com