简体   繁体   English

如何使用JavaScript在HTML中显示图像数组?

[英]How can I display an array of images in HTML using javascript?

I am trying to create a contacts page and trying to populate the contacts image and name. 我正在尝试创建联系人页面,并尝试填充联系人图像和姓名。 I am able to display the array of names but not the array of pictures. 我可以显示名称数组,但不能显示图片数组。 Just the first picture is displayed. 仅显示第一张图片。

I am also trying to align the contact and the image in one row. 我还试图将联系人和图像对齐为一行。 But the contact image is displayed first then the contact name is displayed. 但是首先显示联系人图像,然后显示联系人姓名。

Here goes the code: 代码如下:

<script language="javascript" type="text/javascript">

function showContacts()
{
    var myContacts=["abc","def","xyz"]; // literal array

    for (var i=0;i<myContacts.length;i++)
    {
        document.getElementById('contact').innerHTML += myContacts[i]+"<br>";
        //document.write(myArray[i]);
    }
}
function preloader() 
{
    var myPhoto=["some photos"]; // literal array
    // create object
    var img=document.getElementById('photo');
    // start preloading
    for(var i=0; i< myPhoto.length; i++) 
    {
        img.src += myPhoto[i]+"<br>";
        //document.write(i);
        //img.setAttribute('src',myPhoto[i]); 
     }
 } 

</SCRIPT>


<body onload="showContacts();preloader();">

<table width="100%" style="height: 100%;" border="0"><tr>
    <col colspan="1" ><image id="photo"/>

    <col  colspan="1" ><p id="contact"/>

</tr></table>
</body>
</html>

What am I missing? 我想念什么?

You need to add multiple images to the page. 您需要向页面添加多个图像。 You can do that in javascript. 您可以在javascript中做到这一点。

<table width="100%" style="height: 100%;" border="0">
  <tr>
    <td><p id="photos" /></td>
    <td><p id="contacts" /></td>
  </tr>
</table>


var container = document.getElementById("photos");

for (var i=0, len = myPhoto.length; i < len; ++i) {
     var img = new Image();
     img.src = myPhoto[i];
     container.appendChild(img);
}

UPDATE: this is a simple demo how to add multiple images to the DOM. 更新:这是一个简单的演示如何将多个图像添加到DOM。 What you probably want to achieve is that you have multiple table rows with one name & image per row. 您可能想要实现的是,您有多个表行,每行一个名称和一个图像。 To accomplish that, you have to create/append new rows/cells using document.createElement (or a framework like jQuery). 为此,您必须使用document.createElement(或类似jQuery的框架)创建/添加新的行/单元格。

UPDATE 2 - added a demo which adds multiple rows (one per contact): 更新2-添加了一个演示,该演示添加了多行(每个联系人一个):

http://jsfiddle.net/roberkules/WRgjv/ http://jsfiddle.net/roberkules/WRgjv/

Your HTML is invalid, you need something like: 您的HTML无效,您需要输入以下内容:

<table>
  <colgroup>
    <col ...>
  <colgroup>
    <col ...>
  <tr>
    <td><img id="image0" ...>
    <td><p id="contact0" ...>
  <tr>
    <td><img id="image1" ...>
    <td><p id="contact1" ...>
  ...
</table>

Read the HTML 4.01 specification for table elements and use the W3C validator to check markup. 阅读元素的HTML 4.01规范,并使用W3C验证器检查标记。

The "preloader" script is not doing what you might think, roberkules' answer is on the right track. “预加载器”脚本没有按照您的想法做,roberkules的答案是正确的。

暂无
暂无

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

相关问题 Javascript + HTML:如何有效显示大量图像? - Javascript + HTML: How can I efficiently display a large list of images? 如何在 html 中显示来自 Javascript 数组的随机引用? - How can I display an random quote from an Javascript Array in html? 如何在 html div 中显示多个图像,使用 javascript 的数组和循环 - How to display multiple images in a html div, using javascript's array and loop 有没有办法可以将图像和字符串添加到数组并存储在本地存储中并使用 JavaScript 显示它们? - is there a way I can add images and strings to an array and store on local storage and display them using JavaScript? 如何在javascript数组中显示图像 - how to display images in a javascript Array 如何使用 Javascript 将数组中的元素插入到 HTML 文档中? - How can I insert elements in an array to a HTML document using Javascript? 如何使用javascript在html中上传图像时显示多个缩略图? - How to display multiple thumbnails while uploading images in html using javascript? 如何在使用数组的 javascript 创建的元素中显示图像? - How to display images in elements created with javascript using an array? 如何使用 javascript 循环和数组显示图像幻灯片? - how to display images slide show using javascript loop and array? 我无法使用HTML(innerHTML)中的Javascript显示排序后的数组 - I can't display my sorted array with Javascript in HTML (innerHTML)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM