gpt4 book ai didi

javascript - 为什么我的 JavaScript 不工作?调试器中没有显示任何错误

转载 作者:行者123 更新时间:2023-11-28 15:14:57 25 4
gpt4 key购买 nike

我有一个 HTML 文件,它在页面加载时调用 javascript 函数 randomNumber()。我已经测试了 javascript,并将数字变量设置为在页面加载时发出警报作为测试,并且 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 调试器中没有错误。我做错了什么?

最佳答案

return 之后的代码将不会执行,请尝试将您的函数更改为此。

function randomNumber() {
var number = Math.floor(Math.random()*(audio.length));
document.getElementById('album-cover').src = covers[number];
alert(covers[number]);

return number;
}

阅读 Material

return

The return statement ends function execution and specifies a value to be returned to the function caller.

关于javascript - 为什么我的 JavaScript 不工作?调试器中没有显示任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34472641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com