简体   繁体   English

javascript - 单击按钮时调用函数

[英]javascript - call function when button is clicked

Can someone show me what I am doing wrong here? 有人能告诉我这里我做错了什么吗?

I am trying to execute a function when a button is clicked. 我试图在单击按钮时执行一个功能。

The HTML: HTML:

<button id="btn1">Press me!</button>
<input type="button" id="btn2" value="Press me!"/>​

The javascript: javascript:

var btn1 = document.getElementById('btn1'),
    btn2 = document.getElementById('btn2');


function do() {
    alert('Yay!');
}
btn1.onclick = do;
btn2.onclick = do;​

Here's a demo. 这是一个演示。 I'm not sure why it's not working. 我不确定为什么它不起作用。 It's probobly something way to obvious for me to notice. 对于我来说,这显然是一种显而易见的方式。 :) :)

Change the name of your function from do to something that is not a reserved keyword. 将函数的名称从do更改为非保留关键字的名称。

do is a keyword. do是一个关键字。 If you used a debugger you'd know that. 如果您使用调试器,您就会知道。

do is a keyword. do是一个关键字。 Change variable name and it will work. 更改变量名称,它将起作用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM