如何在我的音频播放器播放mp3文件时传递Javascript变量

How to pass a Javascript variable as the mp3 file my audio player plays

本文关键字:文件 变量 Javascript mp3 播放 我的 音频 播放器      更新时间:2023-09-26

我正在尝试制作一个音频播放器,它从JSONP中获取mp3文件,并将其发送到HTML音频播放器。JSONP没有包含在这段代码中,我将其简化为将mp3文件作为变量包含在内。无论如何,我想知道如何将Javascript变量作为音频播放器的src传递到HTML上。谢谢

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
    <div></div>
    <script>
        var theFile = 'foo.mp3';
        $("div").html("<audio src= theFile controls preload='auto' autobuffer></audio>");                   
    </script>
</body>

$("div").html("<audio src='" + theFile + "' controls preload='auto' autobuffer></audio>");

你能用这个吗?

 $('div.demo-container').html(function() {
      var emph = '<em>' + $('p').length + ' paragraphs!</em>';
      return '<p>All new content for ' + emph + '</p>';
    });

JQuery.html()文档中的更多信息:http://api.jquery.com/html/