【问题标题】:How to get JS data to HTML?如何将 JS 数据转换为 HTML?
【发布时间】:2019-09-26 17:27:00
【问题描述】:

我正在尝试将我的 JSON 数据着色为 HTML。所以我使用 JSON 数据并将其设置为变量。然后我分配了更多变量来访问 JSON 数据中我想要的每个元素的数据。现在,我有变量形式的数据,但我无法使用 document.getElementById 将其传递给 HTML。这可能是因为我对同一个变量有不同的数据,所以只有最后一个元素会打印出 Id,因为它是唯一的。所以,请帮助我如何将 JS 变量数据打印到 HTML 中。

var obj = [{
    "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
    "type": "message",
    "text": "hey there",
    "source_team": "TN4AF0V5W",
    "team": "TN4AF0V5W",
    "user_profile": {
      "real_name": "marvelmohinish99",
      "team": "TN4AF0V5W"
    }
  },
  {
    "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
    "type": "message",
    "text": "welcome",
    "source_team": "TN4AF0V5W",
    "team": "TN4AF0V5W",
    "user_profile": {
      "real_name": "marvelmohinish99",
      "team": "TN4AF0V5W"
    }
  },
  {
    "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
    "type": "message",
    "text": "Help me",
    "source_team": "TN4AF0V5W",
    "team": "TN4AF0V5W",
    "user_profile": {
      "real_name": "marvelmohinish99",
      "team": "TN4AF0V5W"
    }
  }
];

for (var i in obj) {
  var type = obj[i].type;

  var text = obj[i].text;

  var source_team = obj[i].source_team;

  var user_profile = obj[i].user_profile;

  var real_name = user_profile.real_name;


  console.log(source_team + " : " + real_name);

  console.log(text);

  document.getElementsByClassName('a1').innerHTML = text;

}
<h2>Convert a string written in JSON format, into a JavaScript object.</h2>

<p id="output"></p>
<p class="a1"></p>

【问题讨论】:

  • 尝试将var i in obj更改为let i in obj
  • 不,没有用。
  • getElementsByClassName('a1') 返回一个“类似数组”的对象。所以试图改变该对象的innerHTML 属性是行不通的。只是要清楚。您想根据 JSON 中的内容动态生成新元素,还是想修改现有元素?
  • 您使用 getElement(s)ByClassName 然后您尝试将 array.innerHTML 设置为文本,您需要使用 getElement 而不是 getElement(s)
  • let i of obj 遍历对象的属性,i 是属性。

标签: javascript arrays json multidimensional-array


【解决方案1】:

这是一个如何将其放入简单表格的示例。通常,您可以使用.innerHTML.innerText 属性将项目动态添加到您的HTML:

var obj = [{
    "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
    "type": "message",
    "text": "hey there",
    "source_team": "TN4AF0V5W",
    "team": "TN4AF0V5W",
    "user_profile": {
      "real_name": "marvelmohinish99",
      "team": "TN4AF0V5W"
    }
  },
  {
    "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
    "type": "message",
    "text": "welcome",
    "source_team": "TN4AF0V5W",
    "team": "TN4AF0V5W",
    "user_profile": {
      "real_name": "marvelmohinish99",
      "team": "TN4AF0V5W"
    }
  },
  {
    "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
    "type": "message",
    "text": "Help me",
    "source_team": "TN4AF0V5W",
    "team": "TN4AF0V5W",
    "user_profile": {
      "real_name": "marvelmohinish99",
      "team": "TN4AF0V5W"
    }
  }
];

for (var i in obj) {
  var type = obj[i].type;

  var text = obj[i].text;

  var source_team = obj[i].source_team;

  var user_profile = obj[i].user_profile;

  var real_name = user_profile.real_name;

	var message = document.createElement("tr");
  message.innerHTML =
  	"<td>" + type +
    "</td><td>" + text +
    "</td><td>" + source_team +
    "</td><td>" + user_profile +
		"</td><td>" + real_name + "</td>";

  document.getElementsByClassName('a1')[0].append(message);

}
table, th, td{
  border: 1px solid black;
}
<p id="output"></p>
<table class="a1">
  <th>Type</th>
  <th>Text</th>
  <th>Source Team</th>
  <th>User Profile</th>
  <th>Real Name</th>
</table>

【讨论】:

  • 我们可以像一个在另一个没有盒子的情况下线性打印它吗?
  • 我的意思是我们可以通过取消表格样式来做到这一点吗?
  • 当然,您只需更改 HTML 的结构方式。因此,不要使用 tabletd 元素,而是使用 divs 或类似的东西。
  • 我又遇到了问题。
  • for (var i in obj) { var type = obj[i].type; var text = obj[i].text; var source_team = obj[i].source_team; var user_profile = obj[i].user_profile; var real_name = user_profile.real_name; var message = document.createElement("tr"); message.innerHTML = + type + + text + source_team + + user_profile + + real_name; document.getElementsByClassName('a1')[0].append(message); }
猜你喜欢
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
  • 2021-09-26
  • 2015-03-30
  • 2016-02-14
  • 2014-10-28
  • 1970-01-01
  • 2020-02-24
相关资源
最近更新 更多