语音识别:lang 属性

lang 属性是 SpeechRecognition 接口的一个属性,它返回并设置当前 SpeechRecognition 的语言。如果未指定,则默认为 HTML lang 属性值,或者如果该属性未设置,则默认为用户代理的语言设置。

表示当前 SpeechRecognition 的 BCP 47 语言标签的字符串。

示例

此代码摘录自我们的 语音颜色更改器 示例。

js
const grammar =
  "#JSGF V1.0; grammar colors; public <color> = aqua | azure | beige | bisque | black | blue | brown | chocolate | coral | crimson | cyan | fuchsia | ghostwhite | gold | goldenrod | gray | green | indigo | ivory | khaki | lavender | lime | linen | magenta | maroon | moccasin | navy | olive | orange | orchid | peru | pink | plum | purple | red | salmon | sienna | silver | snow | tan | teal | thistle | tomato | turquoise | violet | white | yellow ;";
const recognition = new SpeechRecognition();
const speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
//recognition.continuous = false;
recognition.lang = "en-US";
recognition.interimResults = false;
recognition.maxAlternatives = 1;

// ...

规范

规范
Web 语音 API
# dom-speechrecognition-lang

浏览器兼容性

BCD 表格仅在启用 JavaScript 的浏览器中加载。

另请参阅