11

我正在尝试在Google Chromex-webkit-speech的简单 HTML/JavaScript 扩展中使用新功能。但是,我已经尝试并尝试查看一堆示例,但无法让它成功调用该函数。我见过其他人这样做,我真的不明白为什么我不能。我将 JavaScript 代码放入一个单独的文件中,但我使用 <script src="filename.js"> 这是我的 x-webkit-speech 行...。

<input id="speechInput" type="text" style="font-size:25px;" x-webkit-speech speech onwebkitspeechchange="onChange()" />

现在,如果我更改onChange()alert(this.value),它会执行一个警报框,其中包含语音输入的值。所以我真的不明白为什么我不能只调用另一个函数。我不是最伟大的 JavaScript 或 HTML 程序员,但我对此进行了大量研究。每个人对事物的定义都不一样,而且由于没有可靠的 API,因此很难真正知道谁的形式正确,因为它们似乎都以某种方式工作。

我的onChange功能看起来像

function onChange() { alert("in onChange"); } 

我只是想测试并确保它能够发挥作用,但我什么也没得到。

4

3 回答 3

8

I was playing around with this feature today and actually your code seems to be OK and works for me. The full version would be the following:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Speech</title>
</head>

<script type="text/javascript" charset="utf-8">
    function onChange() {
        alert('changed');
    }
</script>

<body>

    <input id="speechInput" type="text" style="font-size:25px;"
           x-webkit-speech onwebkitspeechchange="onChange()" />

</body>
</html>

Though I did notice that onChange() does not get called if Chrome fails to recognize the speech. I'm using Chrome 11.0.696.28 beta. Also the speech attribute is not necessary if you're targeting only webkit-based browsers like Chrome or Safari. And even if you leave it in, it doesn't work with Firefox 4. Not sure about IE9 since I don't have it.

于 2011-04-05T06:53:07.820 回答
0
if (document.createElement("input").webkitSpeech === undefined) {
    alert("Speech input is not supported in your browser.");
}

你可以使用这个代码

于 2013-04-02T17:25:08.030 回答
-2

别担心,只要尝试遵循它应该会有所帮助

<!DOCTYPE html> 
<html>
<meta charset="utf-8" />  
<title>Speech Input Test</title>  
<h2>Speech Input Test</h2>
<input id="speech-input-field" type="text" x-webkit-speech="">
<html>
于 2012-10-01T14:28:29.157 回答