[英]How can i call javascript function into src element of Audio tag in HTML5
Following is my code where i have written one function in JavaScript and trying to call it in src element of audio tag. 以下是我的代码,我在JavaScript中编写了一个函数,并尝试在音频标记的src元素中调用它。 also want to know that whether return " http://translate.google.com/translate_tts?tl=en&q=this is a test" + File;
还想知道是否返回“ http://translate.google.com/translate_tts?tl=en&q=this is a test”+ File;
statement is ok to get google TTS audio file?
<html>
<body>
<script language="javascript" type="text/javascript">
function myFunction()
{
var File = "this is a test";
return "http://translate.google.com/translate_tts?tl=en&q=this is a test" + File;
}
</script>
<audio id="speech" src="javascript:myFunction();" controls="controls" autoplay="autoplay"></audio>
That won't work because the contents of the 'src' tag will not be parsed as JavaScript, it will be treated as an ordinary string. 这不起作用,因为'src'标签的内容不会被解析为JavaScript,它将被视为普通字符串。
You could move the script tag below the audio tag and then do this below your definition of 'myFunction': 您可以将脚本标记移动到音频标记下方,然后在“myFunction”的定义下面执行此操作:
document.getElementById('speech').src = myFunction();
An even better approach would be to move your JavaScript code into a separate .js file, and set the 'src' attribute in the 'onload' handler for the page, rather than mixing JavaScript source code in with your HTML markup. 更好的方法是将JavaScript代码移动到单独的.js文件中,并在页面的'onload'处理程序中设置'src'属性,而不是将JavaScript源代码与HTML标记混合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.