我有一个HTML文件,在页面加载时调用javascript函数randomNumber()。我已经测试了javascript,并将number变量设置为在页面加载时作为测试进行警告,并且Math.random正在运行,但之后,当尝试提醒cover [number]时,它不起作用。这是我的HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="Log in to your SongBird account with email, Facebook, or Twitter.">
<meta name="keywords" content="Musuc, SongBird, Song, Bird">
<meta name="author" content="HueDrop Studio">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SongBird - Share Music</title>
<link rel="stylesheet" href="dashboard.css" type="text/css">
<link rel="stylesheet" href="mobile-dashboard.css" media="screen and (max-width: 480px)" type="text/css">
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico"/>
<script src="scripts/jquery-1.11.3.min.js"></script>
<script src="scripts/audio-core.js"></script>
</head>
<body onload="randomNumber()">
<img src="" class="album-cover" id="album-cover"/>
<!--<script type="text/javascript"> document.write( "<img class='album-cover' src = '" + covers[randomNumber(audio.length -1)]+ "' alt=''>" );</script>-->
<div class="overlay">
<h1 class="now-playing">Now Playing</h1>
<h1 class="playing-title">Somebody Told Me</h1>
<h2 class="artist">The Killers</h2>
<h2 class="album">Hot Fuss</h2>
<div>
<audio controls autoplay='true'>
<source src="" type="audio/mpeg" id="audio"></source>
<!--<script type="text/javascript"> document.write( "<source type='audio/mpeg' src = '" + audio[randomNumber(audio.length -1)]+ "'>" );</script>-->
Your browser is out of date. You may not be able to listen to audio.
</audio>
</div>
</div>
<div class="nav-bar">
<a href="/dashboard" class="songbird-logo">Song<strong>Bird</strong> </a>
<a href="javascript:void();" class="userProfNavImgLink">
<img src="images/usr/alex.png" class="userProfNavImg" alt=""/>
</a>
<a href="javascript:void();" class="userProfNavLink">Alex Dovzhanyn </a>
</div>
</body>
</html>
这是我的javascript:
var audio = new Array()
audio[0] = "audio/the_killers/hot_fuss/somebody_told_me.mp3"
audio[1] = "audio/panic_at_the_disco/a_fever_you_cant_sweat_out/i_write_sins_not_trag edies.mp3"
var covers = new Array()
covers[0] = "images/album_covers/hot_fuss.jpg"
covers[1] = "images/album_covers/a_fever_you_cant_sweat_out.jpg"
function randomNumber() {
var number = Math.floor(Math.random()*(audio.length));
return number;
document.getElementById('album-cover').src = covers[number];
alert(covers[number]);
}
randomNumber()函数应该从数组音频的长度中选择一个随机数,然后替换html中#album-cover的img src。但是代码似乎打破了这里,在chrome调试器中没有错误。我做错了什么?
答案 0 :(得分:4)
return
之后的代码将无法执行,请尝试将您的功能更改为此。
function randomNumber() {
var number = Math.floor(Math.random()*(audio.length));
document.getElementById('album-cover').src = covers[number];
alert(covers[number]);
return number;
}
阅读材料
return语句结束函数执行并指定要返回给函数调用者的值。
答案 1 :(得分:0)
为什么return
功能中有randomNumber()
?它之后的代码将不会执行。