我对javascript相对较新,因此,我的代码在我的页面加载时无法显示随机图像。我已经看了几个例子来尝试解决问题,但经过几个小时的搜索后,我准备按下帮助按钮。我已经为我的页面添加了HTML和脚本。另外,我已经评论了我试图操纵的IMG标签。
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome in.</title>
<link href="home.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="slides.js"></script>
<script>
//This is where I am attempting to set an random image to load.
function()
{
//Array of quotes
var randomImages= new Array ("Images/quote1.png", "Images/quote2.png","Images/quote3.png", "Images/quote4.png", "Images/quote5.png","Images/quote6.png","Images/quote7.png","Images/quote8.png");
//Random number to select an image. Use 8, not 7, since javascript arrays are 0 based
//Math.floor will take us from a floating point number to an integer base
var randomNumber = Math.floor(7*Math.random());
//Display that image
document.getElementById('quoter').setAttribute('src',randomImages[randomNumber]);
}
</script>
</head>
<body onLoad="function()">
<a href="aboutme.html" id="aboutMe"><img src="Images/aboutme.png" alt="aboutme"/></a>
<a href="aboutme.html" id="contact"><img src="Images/contact.png" alt="aboutme"/></a>
<a href="aboutme.html" id="blog"><img src="Images/blog.png" alt="aboutme"/></a>
<a href="aboutme.html" id="wife"><img src="Images/mywife.png" alt="aboutme"/></a>
<div id="mid">
</div>
<div id="slideshow">
<div>
<img src="Images/dummypic.png" alt="me">
</div>
<div>
<img src="Images/dummypic2.png" alt="me">
</div>
<div>
<img src="Images/dummypic3.png" alt="me">
</div>
</div>
<div>
<img id="quoter" src="" alt="quotes"> <!--This is what I am trying to make random when the page loads-->
</div>
</body>
</html>
答案 0 :(得分:0)
为您的功能提供名称。
它不能是function()
,它应该像function function_name()
一样,然后在身体onLoad上使用<body onLoad="function_name()">
由于您已包含jquery-1.7.1.min.js
,因此您可以尝试使用此代码,并应删除body onload属性。
$(function(){
//Array of quotes
var randomImages= new Array ("Images/quote1.png", "Images/quote2.png","Images/quote3.png", "Images/quote4.png", "Images/quote5.png","Images/quote6.png","Images/quote7.png","Images/quote8.png");
//Random number to select an image. Use 8, not 7, since javascript arrays are 0 based
//Math.floor will take us from a floating point number to an integer base
var randomNumber = Math.floor((randomImages.length-1)*Math.random());
//Display that image
$('#quoter').prop('src',randomImages[randomNumber]);
});
答案 1 :(得分:0)
您已经加载了jQuery,因此请使用它。从正文中删除onload并将脚本更改为:
$(document).ready(function() {
//Array of quotes
var randomImages= ["Images/quote1.png", "Images/quote2.png","Images/quote3.png", "Images/quote4.png", "Images/quote5.png","Images/quote6.png","Images/quote7.png","Images/quote8.png"];
//Random number to select an image. Use 8, not 7, since javascript arrays are 0 based
//Math.floor will take us from a floating point number to an integer base
var randomNumber = Math.floor(7*Math.random());
//Display that image
$('#quoter').attr('src',randomImages[randomNumber]);
});