简体   繁体   English

为什么javascript addEventListener不做任何事情?

[英]Why isn't the javascript addEventListener doing anything?

I'm trying to write a script that moves the orange box around inside the blue box using the arrow keys and Javascript eventlistener. 我正在尝试编写一个脚本,使用箭头键和Javascript事件侦听器在橙色框内移动橙色框。 When I run the page, nothing happens. 当我运行页面时,什么都没有发生。 I've tried poking around in the console, but it isn't giving me any output at all. 我尝试在控制台中四处浏览,但是它根本没有提供任何输出。 I'm sure I'm missing something small but I can't for the life of me figure it out. 我确定我缺少一些小东西,但我一生都无法解决。 Any suggestions would be great! 任何建议都很好!

 var orange = document.getElementById("orange"); orange.addEventListener("onkeydown", move, false); function move(e); e = e || window.event; if(e.keyCode == '38'){ //up if(parseInt(orange.style.top) > '0'){ orange.style.top = parseInt(orange.style.top) - 5; } } else if (e.keyCode == '40'){ //down if(parseInt(orange.style.top) < '425'){ orange.style.top = parseInt(orange.style.top) + 5; } } else if (e.keyCode == '37'){ //left if(parseInt(orange.style.top) > '0'){ orange.style.left = parseInt(orange.style.left) - 5; } } else if (e.keyCode == '39') { //right if(parseInt(orange.style.left) < '425') { orange.style.left = parseInt(orange.style.left) + 5; } } } 
 #blue{ background-color: blue; position: relative; height: 500px; width: 500px } #orange{ background-color: orange; position: absolute; width: 75px; height: 75px; }; 
  <div id="blue"> <div id ="orange" style="left: 30px; top:30px;"></div> </div> 

You should be getting a syntax error in your console because you're not declaring your move function correctly. 您应该在控制台中收到语法错误,因为您没有正确声明move函数。

function move(e);

    e = e || window.event;

should look like this: 应该看起来像这样:

function move(e) {

        e = e || window.event;

You have a couple problems: 您有几个问题:

1) The event is keydown not onkeydown (unless you are adding directly to the object: (eg orange.onkeydown ) 1)事件是keydown而不是onkeydown (除非您直接添加到对象:(例如orange.onkeydown

2) Top is measured in pixels, not just an integer - you need to add + 'px' to your top changes: orange.style.top = parseInt(orange.style.top) + 5 + 'px' 2)top以像素为单位,而不仅仅是整数-您需要在顶部更改中添加+ 'px'orange.style.top = parseInt(orange.style.top) + 5 + 'px'

Other notes: 其他说明:

  • Might work better to have the event listener on window as it will have focus, idk though - didn't test that. 将事件侦听器放在窗口上可能会更好,因为它将具有焦点,但idk-没有进行测试。
  • e.keyCode is a Number, not a String e.keyCode是数字,而不是字符串
  • as @bmceldowney mentioned, you have a syntax error in your function declaration 如@bmceldowney所述,函数声明中存在语法错误

Working version of your code: https://jsfiddle.net/kkhkL065/ 您的代码的工作版本: https : //jsfiddle.net/kkhkL065/

As stated in this documentation page by Mozilla, the possible targets of the keydown event (not onkeydown as erroneously written in your code) are 如Mozilla在此文档页面中所述, keydown事件的可能目标(不是错误地写在代码中的onkeydown )是

Focused element processing the key event, root element if no suitable input element focused 重点元素处理键事件,如果没有合适的输入元素重点,则根元素

So basically only input ( <input> and <textarea> ) or root elements (document or window) can process the event. 因此,基本上只有输入<input><textarea> )或根元素 (文档或窗口)可以处理事件。

Modify your code like this: 像这样修改您的代码:

var orange = document.getElementById("orange");
var move = function(e){
    orange.classList.add("red");
}
window.addEventListener("keydown", move, false);

Here's a link to JSFiddle: https://jsfiddle.net/nbj0Lujw/ 这是JSFiddle的链接: https ://jsfiddle.net/nbj0Lujw/

Hope it helps :) 希望能帮助到你 :)

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

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