使用JavaScript指定图像(随机选择)
大家好:)我试图从变量列表中随机生成一个图像,但没有任何效果。我试过这样的方法:使用JavaScript指定图像(随机选择),javascript,html,Javascript,Html,大家好:)我试图从变量列表中随机生成一个图像,但没有任何效果。我试过这样的方法: document.querySelector("img").src = variables.img; 但它不起作用,因为它改变了我网站上的第一张图片,而不是我希望它改变的图片。我有一张这样的清单: var songs = [ { filename: "http://linktomp3-1.com", title: "title1", album: "---" }, // Song: title1
document.querySelector("img").src = variables.img;
但它不起作用,因为它改变了我网站上的第一张图片,而不是我希望它改变的图片。我有一张这样的清单:
var songs =
[
{ filename: "http://linktomp3-1.com", title: "title1", album: "---" },
// Song: title1
{ filename: "http://linktomp3-2.com", title: "title2", album: "---" },
// Song: title2
{ filename: "http://linktomp3-3.com", title: "title3", album: "---" },
// Song: title3
{ filename: "http://linktomp3-4.com", title: "title4", album: "---" },
];
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;
以下是正文前的表格:
<table width="300">
<tr>
<td width="190">
<b>Made by:</b> <span>Band Title</span><br>
<b>Title:</b> <span id='songTitle'></span><br><br>
<b>Album:</b> <span id='songAlbum'></span>
</td>
<td width="10">
</td>
<td width="100">
<img src="http://www.someimg.com/someimg.jpg"></img>
</td>
</tr>
</table>
<br><br>
<audio class="audio-element" controls="true" preload="none" loop="loop">
<!-- 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>
制作人:乐队名称
标题:
专辑:
您过时的浏览器不支持HTML5
在此处获取Mozilla Firefox>
是否有任何方法可以使用Javascript指定要更改的图像?为要更改的图像指定一个ID,以便您可以像处理span那样专门针对它
<td width="100">
<img id="bandImg" src="http://www.someimg.com/someimg.jpg"></img>
</td>