循环浏览并显示对象中的内容

时间:2019-07-31 02:13:31

标签: javascript object

我正在尝试遍历对象,并以3列布局显示所有对象。

我看到的第一个问题是我覆盖了前两个对象,并且仅显示最后一个对象。我认为这是因为未创建新元素。

关于要显示所有3个对象需要做什么的任何想法?

$(function() {
  const CourseData = {
    "1": {
      thumbnail: "https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80",
      title: "Title 1",
      price: "14.95"
    },
    "2": {
      thumbnail: "https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80",
      title: "Title 2",
      price: "19.95"
    },
    "3": {
      thumbnail: "https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80",
      title: "Title 3",
      price: "4.95"
    }
  };

  for (let courses in CourseData) {
    document.getElementById("image").setAttribute("src", CourseData[courses].thumbnail);
    document.getElementById("title").innerHTML = CourseData[courses].title;
    document.getElementById("price").innerHTML = CourseData[courses].price;
  }
});
<html>

<head>
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">


</head>

<body>
  <div id="dynamic"></div>
  <div class="container">
    <div class="card col-sm-12 col-md-6 col-lg-3">
      <div class="hover">
        <a class="figure" href="register/index.php?course=101">
          <img id="image" class="img-fluid card-img-top" alt="hazing prevention thumbnail">
        </a>
      </div>
      <div class="card-body">
        <a id="title" href="register/index.php?course=101" class="course-link">Course Title</a>
      </div>
      <div class="card-footer">
        <p id="price" class="price mb-0">Course Price</p>
      </div>
    </div>
  </div>


  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  <script src="course-data.js"></script>
</body>

</html>

1 个答案:

答案 0 :(得分:1)

由于已经加载了jQuery,因此可以使用简单的模板文字使用$.append将所需的HTML添加到容器中。

$(function() {
  const CourseData = {
    "1": {
      thumbnail: "https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80",
      title: "Title 1",
      price: "14.95"
    },
    "2": {
      thumbnail: "https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80",
      title: "Title 2",
      price: "19.95"
    },
    "3": {
      thumbnail: "https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80",
      title: "Title 3",
      price: "4.95"
    }
  };


  const container = $("#cards-container");
  for (let courses in CourseData) {
    const {
      thumbnail,
      title,
      price
    } = CourseData[courses];
    const template = `<div class="card col-sm-12 col-md-6 col-lg-3">
      <div class="hover">
        <a class="figure" href="register/index.php?course=101">
          <img src="${thumbnail}" class="img-fluid card-img-top" alt="hazing prevention thumbnail">
        </a>
      </div>
      <div class="card-body">
        <a href="register/index.php?course=101" class="course-link">${title}</a>
      </div>
      <div class="card-footer">
        <p class="price mb-0">${price}</p>
      </div>
    </div>`;
    container.append(template);

  }
});
<html>

<head>
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">


</head>

<body>
  <div id="dynamic"></div>
  <div id="cards-container" class="container">
  </div>


  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  <script src="course-data.js"></script>
</body>

</html>

此外,如果您不想使用jQuery,则可以将HTML字符串附加到innerHTML。

$(function() {
  const CourseData = {
    "1": {
      thumbnail: "https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80",
      title: "Title 1",
      price: "14.95"
    },
    "2": {
      thumbnail: "https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80",
      title: "Title 2",
      price: "19.95"
    },
    "3": {
      thumbnail: "https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80",
      title: "Title 3",
      price: "4.95"
    }
  };


  const container = document.getElementById("cards-container");
  for (let courses in CourseData) {
    const {
      thumbnail,
      title,
      price
    } = CourseData[courses];
    const template = `<div class="card col-sm-12 col-md-6 col-lg-3">
      <div class="hover">
        <a class="figure" href="register/index.php?course=101">
          <img src="${thumbnail}" class="img-fluid card-img-top" alt="hazing prevention thumbnail">
        </a>
      </div>
      <div class="card-body">
        <a href="register/index.php?course=101" class="course-link">${title}</a>
      </div>
      <div class="card-footer">
        <p class="price mb-0">${price}</p>
      </div>
    </div>`;
    container.innerHTML += template;

  }
});
<html>

<head>
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">


</head>

<body>
  <div id="dynamic"></div>
  <div id="cards-container" class="container">
  </div>


  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  <script src="course-data.js"></script>
</body>

</html>

请注意,我已经删除了卡上DOM元素上的ID,因为将有多个此类元素,并且ID的目的也将丢失。