简体   繁体   English

为什么在 javascript 中使用 onkeydown 时,function() 需要在 function 名称之前键入?

[英]why does function() need to be typed before the function name when using onkeydown in javascript?

I'm pretty new to javascript, and I was practicing a bit with events on W3Schools.我是 javascript 的新手,我在 W3Schools 上练习了一些活动。 In their tutorial, they show this line of code:在他们的教程中,他们展示了这行代码:

 document.getElementById("demo").onkeydown = function() {myFunction()}; function myFunction() { document.getElementById("demo").style.backgroundColor = "red"; }

I don't understand why you have to type function() {function name()} rather than just typing the function you are trying to call.我不明白为什么您必须键入 function() {function name()} 而不是只键入您要调用的 function。 Basically, I'm not understanding the logic behind that line of code.基本上,我不理解那行代码背后的逻辑。 Can someone please explain it to me?有人可以向我解释一下吗?

You don't need function , you can just put the function name there.您不需要function ,只需将 function 名称放在那里即可。

 document.getElementById("demo").onkeydown = myFunction; function myFunction() { document.getElementById("demo").style.backgroundColor = "red"; }
 <input id="demo">

You would need to wrap it inside another function if you wanted to do more than just call the function directly.如果你想做的不仅仅是直接调用 function,你需要将它包装在另一个 function 中。

 document.getElementById("demo").onkeydown = function() { console.log("Keydown"); myFunction(); }; function myFunction() { document.getElementById("demo").style.backgroundColor = "red"; }
 <input id="demo">

You can do it , you must pass it as reference:你可以做到,你必须将它作为参考传递:

document.getElementById("demo").onkeydown = myFunction(); - myFunction will be called immediately and not as callback. - myFunction将被立即调用而不是作为回调。 Callback would be return value of myFunction .回调将是myFunction的返回值。

You them assign it as reference:您将其指定为参考:

document.getElementById("demo").onkeydown = myFunction - note that you can't pass parameters at runtime here! document.getElementById("demo").onkeydown = myFunction - 请注意,您不能在此处在运行时传递参数!

In the example you give:在您给出的示例中:

 document.getElementById("demo").onkeydown = function() {myFunction()}; function myFunction() { document.getElementById("demo").style.backgroundColor = "red"; }

the value of the onkeydown property of an element must be a function - not a call to a function. So you could not have for example:元素的 onkeydown 属性的值必须是 function - 而不是对 function 的调用。所以你不能有例如:

  document.getElementById("demo").onkeydown = myFunction();

What that would do would be to try to run myFunction and put the result from that into the onkeydown property.这样做的目的是尝试运行 myFunction 并将其结果放入 onkeydown 属性中。

Actually it would fail as at the time the system tried to run it that function doesn't yet exist.实际上它会失败,因为在系统尝试运行它时 function 尚不存在。

What you could do is declare the function first and then use that as the value for the assignment.您可以做的是先声明 function,然后将其用作赋值的值。

function myFunction() {
  document.getElementById("demo").style.backgroundColor = "red";
}
document.getElementById("demo").onkeydown = myFunction;

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

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