使用getElementById时的空格(HTML5站点,使用Javascript)

时间:2015-02-14 14:45:45

标签: javascript html5

我有这段代码:

<b>Title</b>: <span id='songTitle'></span><br>
<b>Album</b>: <span id='songAlbum'></span><br>

<script>
var songs =
[
    { filename: "title1.mp3", title: "title1", album: "Album1" },
    { filename: "title2.mp3", title: "title2", album: "Album2" },
    // add more songs here...
];

var randomIndex = Math.floor(Math.random() * songs.length);
var song = songs[randomIndex];
document.querySelector("source").src = song.filename;
document.getElementById("songTitle").textContent = song.title;
document.getElementById("songAlbum").textContent = song.album;
</script>

<audio class="audio-element" controls="true" preload="none">

<!-- Adding audio sources -->

            <source src="mp3file.mp3" type="audio/mpeg" />
<br>
<b>Your outdated browser does not support HTML5. <br>
Get Mozilla Firefox <a href="https://www.mozilla.org/pl/firefox/new/"> >HERE< </a></b>

</audio>

我希望它显示从列表中随机生成的标题和专辑文本,但它不起作用:(只有空格,没有任何内容写在&#34;:&#34;标记,那里应该是&#34; title1&#34;和&#34; Album1&#34;例如。

有谁能告诉我什么是错的?

1 个答案:

答案 0 :(得分:0)

你应该在音频标签之后向下移动你的javascript,否则在浏览器中呈现标记之前它会被触发。理想情况下,您可以在身体标签关闭之前放置它

<body>
<!-- Markup -->
    <script ...>...</script>
</body>