HTML5之語音識別實例 代碼 <input type="text" x-webkit-speech id="d1" lang="zh-CN" x-webkit-grammar="bUIltin:search" onwebkitspeechchange="foo()"/> <script> function foo(){ var n = document.getElementById("d1").value; if(n == "百度"){ window.location.; }else{ window.location.; } } </script> 說明: 1)x-webkit-speech:語音識別支持屬性 <input type="text" x-webkit-speech/> 2)lang:設(shè)置語言種類,比如漢語:lang="ch-CN" <input type="text" x-webkit-speech lang="ch-CN"/> 3) x-webkit-grammar :語音輸入語法 比如: x-webkit-grammar="bUIltin:search"使得語音輸入的內(nèi)容盡量靠近搜索內(nèi)容,去除多余的字符,例如“的、啦”等 <input type="text" x-webkit-speech lang="ch-CN" x-webkit-grammar="bUIltin:search"/> 4) onwebkitspeechchange :語音輸入事件,當(dāng)語音改變時觸發(fā) 比如:onwebkitspeechchange="foo()" ,當(dāng)停止語音時,會觸發(fā)js中的foo()函數(shù) <input type="text" x-webkit-speech lang="ch-CN" x-webkit-grammar="bUIltin:search" onwebkitspeechchange="foo()"/> 此時,需要寫相應(yīng)的JavaScript函數(shù)foo() <script> function foo(){ //函數(shù)體,如下: alert(8); } </script> |
|