简体   繁体   English

我想在变量中存储一个 function 并想在 onclick 事件中调用它

[英]I want to store a function in variable and want to call that at onclick event

 var down_1 = document.getElementById('GFG_DOWN_1'); var fun = funto(); function funto() { down_1.innerHTML = 'From function 1'; }
 <h1 style="color:green;">GeeksforGeeks</h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"></p> <button onclick="fun">click here</button> <p id="GFG_DOWN_1" style="color:green; font-size: 20px; font-weight: bold;">

If you want to set variable to event, you can use addEventListener as如果要将变量设置为事件,可以使用addEventListener作为

document.getElementById('click').addEventListener('click',funto);

Your code var fun = funto() need change to var fun = funto;您的代码var fun = funto()需要更改为var fun = funto;

 var down_1 = document.getElementById('GFG_DOWN_1'); var fun = funto; function funto() { down_1.innerHTML = 'From function 1'; } document.getElementById('click').addEventListener('click',funto);
 <h1 style="color:green;">GeeksforGeeks</h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"></p> <button id="click">click here</button> <p id="GFG_DOWN_1" style="color:green; font-size: 20px; font-weight: bold;">

First, welcome to the community.首先,欢迎来到社区。 Second, I would suggest you simplify your code.其次,我建议您简化代码。 Instead of having variable outside function put them inside of it.而不是在 function 之外有变量,而是将它们放在其中。 Of course, if you ain't using them anywhere else.当然,如果您不在其他任何地方使用它们。

So the JS code may look something like this:所以 JS 代码可能看起来像这样:

 function fun() {
      document.getElementById('GFG_DOWN_1') = 'From function 1';
    }

and HTML, then, should look like this:那么 HTML 应该如下所示:

<h1 style="color:green;">GeeksforGeeks</h1>
<p id="GFG_UP" style="font-size: 15px; font-weight: bold;"></p>
<button onclick="fun();">click here</button>
<p id="GFG_DOWN_1" style="color:green; font-size: 20px; font-weight: bold;">

Please notice that you are calling a function in onclick , not a variable.请注意,您在 onclick 中调用onclick ,而不是变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在div onClick函数中为图像禁用onClick事件? - I want to disabled onClick event for image inside div onClick function? 我想在 onclick 上调用 function 但 function 应该在另一个 ZC1C424574AB11 内 - I want to call a function on onclick but that function should be inside another function 我想在同一个 onclick 中调用两个 const - I want to call two const in the same onclick 我想将此随机函数更改为onclick函数 - I want to change this random function to onclick function 在 onclick 事件中,我希望将变量传递给 div(我使用的是单个 div) - In onclick event I want the pass the variable to div (I'm using single div) 如何在onClick函数中编写JavaScript代码以及我也想调用另一个函数 - How can I write JavaScript code in onClick function as well as I also want to call another function 我想将变量数组存储为 $_SESSION 变量 - I want to store an array of variables as a $_SESSION variable 将onclick事件添加到javascript中新创建的表格标题中,但是onclick函数被添加到页面上的所有内容中,而不仅仅是我想要的一个 - Adding onclick event to newly created table header in javascript, but the onclick function is added to all th's on the page, not just the one I want 单击按钮,我希望按钮被着色以及存储值 - onclick on button,i want the button to be coloured as well as store a value 我想用 onclick 事件停止 javascript 倒数计时器 - i want to stop javascript countdown timer with onclick event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM