[英]JavaScript - audio src - replace URL with variable
I've looked up similar questions, but haven't been able to make this work. 我查询了类似的问题,但未能完成这项工作。 Sorry if this is a basic question.
抱歉,这是一个基本问题。
This works for me with an on-click button: 通过单击按钮,这对我有用:
var player = document.createElement('audio');
player.src = 'http://jplayer.org/audio/ogg/Miaow-07-Bubble.ogg';
player.load();
player.play();
How can I replace the URL with a variable - "sound1", where the variable fetches a different URL? 如何使用变量“ sound1”替换URL,而该变量获取另一个URL? I tried this for the 2nd line, to no avail:
我尝试了第二行,但无济于事:
player.src = "'" + sound1 +"'";
Thanks. 谢谢。
You don't need "
or '
. Just use 您不需要
"
或'
。只需使用
player.src = sound1;
Assuming that sound1 just holds an URL. 假设sound1仅包含一个URL。
For example: 例如:
var sound1 = 'http://jplayer.org/audio/ogg/Miaow-07-Bubble.ogg'; var player = document.createElement('audio'); player.src = sound1; player.load(); player.play();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.