简体   繁体   English

如何使用JavaScript在页面加载中加载图像?

[英]How can I load images on page load with Javascript?

I'm a student and new to javascript trying to load the images on my webpage using javascript. 我是一名学生,刚接触javascript,尝试使用javascript将图像加载到我的网页上。 I tried to put the image sorce in an array and made for loop that would load images on the the site. 我试图将图像存储在一个数组中,并进行for循环以将图像加载到站点上。 But i can't seem to make it work. 但我似乎无法使其正常工作。 I'll post my code. 我将发布我的代码。

<!DOCTYPE html>
<html>
<head>
<title> Lucky lottery</title>
<link rel="stylesheet" href="theme.css">
<script> src = "imageLoader.js"</script>

</head>
<body>
<div id='container'>
<div id='header'>

    <div id=title><h1 id='titlehead'> Lucky Lottery</h1></div>

</div>

<div id=gallery>

    <div class=images id=image_0></div>
    <div class=images id=image_1></div>
    <div class=images id=image_2></div>
    <div class=images id=image_3></div>
    <div class=images id=image_4></div>
    <div class=images id=image_6></div>
    <div class=images id=image_7></div>
    <div class=images id=image_8></div>

</div>
<div id=subfooter>
    <div class=input>Please fill in your name</div>
    <div id=win_btn><a href="message.js" class="button2">win </a></div>

</div>
<div id=footer>
    <p>

    <h2> Please Sign Up for only 10 dollars. Maybe you
        win one of the amazing products. </h2></p>

</div>

JavaScript: JavaScript:

function imageLader() {
    var imageLijst = ["../images/melk.png", "../images/wasmiddel.png", "../images/bike.png", "../images/holiday.png", "../images/lego-starwars.png",
        "../images/electrische-tandenborstel.png", "../images/my-little-pony.png", "../images/wii-u.png"];

    for(var i = 0; i <imageLijst.length; i++) {
         var imgDiv = document.getElementById("image_"+ i);
         var img = new Image();
         img.src = imageLijst [i];
         imgDiv.appendChild(img);
    }
}

window.onload = imageLader;
<script> src = "imageLoader.js"</script>

is invalid. 是无效的。 That should be 那应该是

<script src="imageLoader.js"></script>

instead. 代替。

Also, though not shown here, just assigning to window.onload is dangerous, as it may be overwritten. 另外,尽管未在此处显示,但仅将其分配给window.onload是危险的,因为它可能会被覆盖。

If you can, elaborate on how its not working. 如果可以,请详细说明其不起作用。 Is there an error (ie a JavaScript error) or is the behavior not as you expect (ie it appears to be doing nothing)? 是否有错误(即JavaScript错误)或行为是否不符合您的预期(即似乎无所事事)?

I would suspect that in this instance its not doing anything because of the following line. 我怀疑在这种情况下,由于以下原因,它什么也没做。

<script> src = "imageLoader.js"</script>

The "src" attribute needs to be defined as a part of the script tag. 需要将“ src”属性定义为脚本标签的一部分。 Trying changing it to this. 尝试将其更改为此。

<script type="text/javascript" src="imageLoader.js"></script>

You are missing lots of quotation marks in your html markup 您在html标记中缺少很多引号

Example: 例:

<div class=images id=image_0></div>

should be 应该

<div class="images" id="image_0"></div>

or 要么

<div class='images' id='image_0'></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM