简体   繁体   English

如何在HTML5中将javascript函数调用到Audio标签的src元素中

[英]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.

相关问题 如何判断HTML5音频元素是否正在播放Javascript - How can I tell if an HTML5 Audio element is playing with Javascript 如何使用angularjs在HTML5音频标签中提供src值 - How to give src value in HTML5 audio tag using angularjs 如何使用JavaScript控制HTML5音频标签 - How to control HTML5 audio tag with javascript 我发现了如何更改html5源代码的视频源(src),但在函数内部或使用内联JavaScript却得到了不同的结果 - I found how to change the video source (src) of an html5 source tag but am getting different results inside a function or with inline javascript 如何使用javascript自动播放html5音频标签 - How to use a javascript to autoplay a html5 audio tag 使用createObjectURL项目数组选择HTML5音频标签的src - Using an array of createObjectURL items to select the src for HTML5 audio tag HTML5 音频标签动态src ajax 它不播放 - HTML5 audio tag dynamic src ajax it does not play 完成后,HTML5音频标记更改Src。 jQuery的 - HTML5 Audio Tag Changing Src when finished. jQuery 我如何 select 与 JavaScript 最接近的 HTML5 元素? - How can I select the closest HTML5 element with JavaScript? 如何让用户使用HTML5导航和播放播放列表 <audio> 标签? - How can I let a user navigate and play a playlist using the HTML5 <audio> tag?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM