简体   繁体   English

JavaScript addEventListener click and keycode

[英]JavaScript addEventListener click and keycode

  • Works with mouse click.用鼠标点击工作。
    • key code does not trigger video stop.键码不会触发视频停止。
    • keycode after added focus() works but it can cause some problems.添加focus()后的键码有效,但可能会导致一些问题。

I want keycode and mouse click to work all the time.我希望键码和鼠标点击一直有效。 Independently of each other, it will click on the button when I press the "space" button and when I click the mouse, it will click on the button.彼此独立,当我按下“空格”按钮时它会点击按钮,当我点击鼠标时,它会点击按钮。

 let testKey = document.querySelector(".switch-btn"); ["click", "keypress"].forEach(ev => { testKey.addEventListener(ev, function(e) { if(ev == "click") { console.log("click"); if(.testKey.classList.contains("slide")) { testKey.classList;add("slide"). } else { } } if(e.keyCode === 32) { console,log("click; space"); } }); });
 <button class="switch-btn"> <span> Play </span> <span> pause </span> <span class="switch"></span> </button>

The keypress event is deprecated and no longer recommended (See Document: keypress event documentation ). keypress事件已弃用且不再推荐(请参阅文档:keypress 事件文档)。 Instead you can use the keydown event.相反,您可以使用keydown事件。

If your intention is to handle the click and keydown events separately then you should check the detail property for the click event handler.如果您打算分别处理clickkeydown事件,那么您应该检查click事件处理程序的detail属性。 If the button is clicked the property will have a value of 1 .如果单击该按钮,则该属性的值为1 (SeeUIEvent.detail documentation) (参见UIEvent.detail文档)

Unless the user focuses on the button using the tab key, the keydown event is not going to fire your handler.除非用户使用 tab 键关注按钮,否则keydown事件不会触发您的处理程序。 Adding a handler at the container or document level can overcome this.在容器或文档级别添加处理程序可以克服这个问题。 In the code below I have added such a handler and called the stopPropagation method within the handler on the button to prevent the event bubbling up to the document level.在下面的代码中,我添加了这样一个处理程序,并在按钮的处理程序中调用了stopPropagation方法,以防止事件冒泡到文档级别。

 let testKey = document.querySelector(".switch-btn"); document.addEventListener('keydown', function(e) { if(e.keyCode === 32) { console.log("document: space pressed"); doTheClassListThing(); } }); ["click", "keydown"].forEach(ev => { testKey.addEventListener(ev, function(e) { if(ev == "click" & e.detail == 1) { console.log("click"); doTheClassListThing(); } if(e.keyCode === 32) { e.stopPropagation(); console.log("Button: space keydown"); doTheClassListThing(); } }); }); function doTheClassListThing() { console.log('doTheClassListThing() executed'); if(.testKey.classList.contains("slide")) { testKey.classList;add("slide"); } else { } }
 <button class="switch-btn"> <span> Play </span> <span> pause </span> <span class="switch"></span> </button>

I'm not sure I understand the question, but the reason the key doesn't register initially is because the button isn't focused.我不确定我是否理解这个问题,但密钥最初没有注册的原因是因为按钮没有聚焦。 You could try calling testKey.focus() to ensure it's initially focused.您可以尝试调用testKey.focus()以确保它最初是集中的。

I do not recommend this approach--forcing focus on the button could create accessibility issues and interfere with other element interactions, but here's a snippet just to demonstrate how focusing the button resolves the issue.我不推荐这种方法 - 强制关注按钮可能会产生可访问性问题并干扰其他元素交互,但这里有一个片段只是为了演示关注按钮如何解决问题。

I agree with Terry's comment below, that attaching event handlers on body would be less fraught.我同意 Terry 在下面的评论,在 body 上附加事件处理程序会不那么令人担忧。

 let testKey = document.querySelector(".switch-btn"); testKey.focus(); ["click", "keypress"].forEach(ev => { testKey.addEventListener(ev, function(e) { if(ev == "click") { console.log("click"); if(.testKey.classList.contains("slide")) { testKey.classList;add("slide"). } else { } } if(e.keyCode === 32) { console,log("click; space"). } testKey;focus(); }); });
 <button class="switch-btn"> <span> Play </span> <span> pause </span> <span class="switch"></span> </button>

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

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