将映射连接到json文件以进行循环

时间:2016-03-24 12:33:28

标签: javascript jquery arrays json loops

这可能是一个非常简单的问题,但我似乎无法在任何地方找到答案或解释。

我如何以及在何处连接地图(图像文件来自哪里) 对于我放在我的JSON中的图像和名称,因为图像需要在我的浏览器窗口中显示。 我是JSON和循环和JS的新手。

这是我的代码,如果您需要查看它,了解我是如何制作的。

JSON;

{ "main_object": {

    "imagesJ": ["beak", "cat", "egg", "meel", "milk", "passport", "spoon", "thee"],
    "woorden": ["words", "not", "needed", "yet"]
 }
}

//JavaScript

var jsonData = "noJson";
var hr = new XMLHttpRequest();

$(document).ready(function() {
  var jsonData = 'empty';
  $.ajax({
    async: false,
    url: "./js/data.json",
    dataType: 'html',
    success: function(response) {
      jsonData = JSON.parse(response);

      console.log('ok');
      imagesJ = jsonData.main_object.imagesJ;
      woorden = jsonData.main_object.woorden;

      for (i = 0; i < imagesJ.length; i++) {
        imagesJ[i] + '.jpg';

        // => /img/[imagesJ[x]].jpg
      }
    },
    error: function() {
      console.log('JSON could not be loaded.');
    }
  });
  console.log(jsonData);


});
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Sleepopdracht</title>
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="css/css.css">
</head>

<body>
  <header>

  </header>

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

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

</html>

2 个答案:

答案 0 :(得分:1)

如果我没弄错的话,你只需要创建一个“img”元素,设置它的“src”属性并附加到“images”节点;

以下是你如何做到这一点

var titles = ["words", "not", "needed", "yet"];
var imagesContainer = document.getElementById('images');

titles.forEach(function(title){
    var img = document.createElement('img');
    img.src = '/img/' + title + '.jpg';
    imagesContainer.appendChild(img);
});

示例:https://jsfiddle.net/vbesoc75/

<强> EDITED

对于你的具体例子:

$.ajax({
     async: false,
     url: "./js/data.json",
     dataType: 'html',
     success: function(response) {
       jsonData = JSON.parse(response);
       console.log('ok');
       imagesJ = jsonData.main_object.imagesJ;
       woorden = jsonData.main_object.woorden;

       var imagesContainer = document.getElementById('images');
       for (i = 0; i < imagesJ.length; i++) {
         var img = document.createElement('img')
         img.src = '/img/' + imagesJ[i];
         imagesContainer.appendChild(img);
       }
    },

答案 1 :(得分:1)

您想要的是为imagesJ中的每个图片添加image tag。您的循环结构很适合,您可以将jQuery to append()图像标记用于div id="images"。 for循环看起来像这样:

for (i = 0; i < imagesJ.length; i++) {
    var path = imagesJ[i] + '.jpg';
    $('#images').append('<img id="image' + i + '" src="' + path + '" />');
}