1

我对 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>
4

2 回答 2

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]);
});​
于 2012-10-23T03:12:28.540 回答
0

为您的函数提供一个名称。

不可能function(),应该是这样 function function_name(),然后在body 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]);
});
于 2012-10-23T03:09:43.463 回答