简体   繁体   English

Javascript 语音识别触发按钮

[英]Javascript Speech Recognition to trigger a button

I am a newbie in Javascript Speech Recognition and chose to develop using Annyang library.我是Javascript Speech Recognition的新手,选择使用Annyang库进行开发。 I want to trigger the "show date" button (without clicking the button) when the user says 'hello'.当用户说“你好”时,我想触发“显示日期”按钮(不点击按钮)。 However, it didn't work.但是,它没有用。 Below is my source codes.下面是我的源代码。 Need some help and thank you.需要一些帮助,谢谢。

 <script src="https://cdnjs.cloudflare.com/ajax/libs/annyang/1.1.0/annyang.min.js"></script> <button type="button" id="subject" onclick="getElementById('demo').innerHTML=Date()">Show Date</button> <p id="demo"></p> <script> window.onload = function() { if (annyang) { var commands = { 'Hello': function() { document.querySelectorAll('#subject').click(); } }; annyang.addCommands(commands); annyang.start(); } } </script>

Try replacing document.querySelectorAll with document.getElementById .尝试用document.querySelectorAll替换document.getElementById

querySelectorAll returns an array of elements, not just a single element (which is what getElementById does). querySelectorAll返回一个元素数组,而不仅仅是一个元素(这是getElementById所做的)。 So you are actually calling click() on an array and not on the button.所以你实际上是在数组而不是按钮上调用click() You can't "click" an array.你不能“点击”一个数组。

There may be other problems in the code as well, I haven't run it myself, but that issue jumped at me.代码中可能还有其他问题,我自己没有运行过,但这个问题突然出现在我身上。

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

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