简体   繁体   English

div的构造函数返回未定义的元素

[英]Constructor function for div returns undefined elements

I have a page I'm trying to refactor using a constructor function rather than preloading several hidden divs. 我有一个页面,我正在尝试使用构造函数重构而不是预加载几个隐藏的div。 I've never used constructors before and I'm using this Code Review post as a guide. 我以前从未使用过构造函数,并且正在使用此Code Review帖子作为指南。 The only difference is that my div is being created on page load. 唯一的区别是我的div是在页面加载时创建的。

The data is stored as JSON and I find the correct object index by matching the ID. 数据存储为JSON,我通过匹配ID找到正确的对象索引。 When the data from the matched object is passed to the constructor, each variable is undefined in the console and I cannot figure out why. 当来自匹配对象的数据传递给构造函数时,控制台中每个变量都是未定义的,我无法弄清原因。

data.json data.json

{
  "missions": [
    { "id":"miss1",
      "content": {
        "name": "Mission 1",
        "desc": "This is a string description for a mission",
        "longDesc": "This is the mission's lond description in case a short one is used someplace else.",
        "img": "https://via.placeholder.com/250",
        "vid": "https://via.placeholder.com/840x430"
        }
      },
     ...
  ]
}

index.html 的index.html

<!-- ... -->
<div class='card--small' id='miss1'><h2>Mission 1</h2></div>
<!-- repeat _n_ times... -->

<div id="card-large></div>

script.js 的script.js

function Card(longDesc, img, video) {
    this.desc = longDesc;
    this.img = img;
    this.video = video;

    console.log(this.desc) // undefined

    var container = document.getElementById("card-large");
    var card = document.createElement("div");
    card.className = "card";

    var img = document.createElement('div');
    img.className = "card__img";
    img.style.backgroundImage = "url(" + this.img + ")";
    card.appendChild(img);

    var bio = document.createElement('div');
    bio.className = "card__bio";
    bio.innerHTML = this.desc

    this.create = function() {
      container.appendChild(card);
    }
  }

  $(".card--small").click(function() {

    var card = new Card();

    var id = $(this).attr('id');

    $.getJSON("data.json").done(function(data) {
      for(var j=0; j<data.missions.length; j++) {
        if(id == data.missions[j].id) {
          card.create(data.missions[j].content.longDesc, data.missions[j].content.img, data.missions[j].content.vid);
        }
      }
    })
  })

I tried serializing/stringifying the data passed to the function, but that didn't help. 我尝试对传递给函数的数据进行序列化/字符串化,但这无济于事。 What am I doing wrong? 我究竟做错了什么? Am I overcomplicating this? 我使这个复杂化了吗?

So a few stuff is incorrect: 因此,有些东西是不正确的:

The constructor is "kinda" correct, only only difference you should have to do is moving the arguments you are sending into "card.create(...)" should be sent into "new Card(...)" 构造函数是“ kinda”正确的,只有区别是将要发送的参数移到“ card.create(...)”中应发送到“ new Card(...)”中

... = the arguments. ... =参数。

Above is for one element tho, as you wanna use a loop instead pretty much all your code in the card method should be moved into the create method. 上面是一个元素,因为您想使用循环,所以card方法中的所有代码几乎都应该移到create方法中。 Ofc this is not a good way to do it, but it's the way to solve the issue you currently got. 这不是一个好方法,但它是解决您当前遇到的问题的一种方法。

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

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