简体   繁体   English

在 keydown 事件中未单击按钮

[英]Button isn't clicked in a keydown event

phptester.net is a site that let's you test PHP codes online. phptester.net是一个让您在线测试 PHP 代码的站点。 You write the PHP code in a dedicated box, and then click a button with the text "Click to test your PHP code" and the result will appear in a adjacent box.您在专用框中编写 PHP 代码,然后单击带有文本“单击以测试您的 PHP 代码”的按钮,结果将出现在相邻的框中。

Testing each change with the keyboard is faster and more convenient to me, so I tried this code to do it by pressing CTRL + A :使用键盘测试每个更改对我来说更快更方便,因此我尝试通过按CTRL + A来执行此代码:

document.addEventListener('keydown', (event)=>{
  if (event.ctrlKey && event.keyCode === 65) {
    document.querySelector('.actionItem button').click();
  }
});

The code fails for some reason --- I mean, I could fill in some PHP code, and hit CTRL + A but the button won't be clicked.代码由于某种原因失败 --- 我的意思是,我可以填写一些 PHP 代码,然后按CTRL + A但不会单击该按钮。 Each time I hit CTRL + A there are no errors in the console, so I don't know what to do with it.每次我按CTRL + A时,控制台中都没有错误,所以我不知道该怎么办。

This is the PHP code I used:这是我使用的PHP代码:

<?php
    $a=5;
    $b=10;
    if ($a < $b) {
        echo "$a is smaller than $b";   
    } else {
        echo "$a IS NOT smaller than $b";   
    }
?>

You got the wrong querySelector, the following works for me:你得到了错误的 querySelector,以下对我有用:

document.addEventListener('keydown', (event)=>{
  if (event.ctrlKey && event.keyCode === 65) {
    document.querySelector('.actionItem input.cbutton').click();
  }
});

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

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