SpeechSynthesisUtterance:lang 属性
lang
属性是 SpeechSynthesisUtterance
接口的一部分,用于获取和设置话语的语言。
值
表示 BCP 47 语言标签的字符串。
示例
js
const synth = window.speechSynthesis;
const inputForm = document.querySelector("form");
const inputTxt = document.querySelector("input");
const voiceSelect = document.querySelector("select");
const voices = synth.getVoices();
// ...
inputForm.onsubmit = (event) => {
event.preventDefault();
const utterThis = new SpeechSynthesisUtterance(inputTxt.value);
const selectedOption =
voiceSelect.selectedOptions[0].getAttribute("data-name");
for (let i = 0; i < voices.length; i++) {
if (voices[i].name === selectedOption) {
utterThis.voice = voices[i];
}
}
utterThis.lang = "en-US";
synth.speak(utterThis);
inputTxt.blur();
};
规范
规范 |
---|
Web 语音 API # dom-speechsynthesisutterance-lang |
浏览器兼容性
BCD 表格仅在启用 JavaScript 的浏览器中加载。