【发布时间】:2018-02-01 15:08:50
【问题描述】:
我正在寻找迭代 JSON 数组并在一行中打印结果(3 个相等的列)。实际的方法不尊重引导背后的逻辑,所以我怎么能告诉 JS 只包含 3 个元素然后克隆一个类似的 div。这是一个可运行的代码,因此您可以对问题有所了解。应该在大屏幕上观看。谢谢。
var url = "https://api.openaq.org/v1/countries?order_by=count&sort=desc";
$.getJSON(url, function (data) {
$.each(data.results, function(i, result) {
$('#data').append(
$('<h2>').text(result.name),
$('<div>').text("Code = " + result.code),
$('<div>').text("Score = " + result.count),
$('<hr>').text(" "),
);
});
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<div class="container">
<h1></i>Open, real-time and historical air quality data</h1>
<hr>
<div class="row" style="background-color: #ddd">
<div class="col-md-4" id="data" style="background-color: #fe8000" ></div>
</div>
<h6 style="padding-bottom: 25px">Source: This data is licensed under the Creative Commons Attribution 4.0 Generic License. It is attributed to OpenAQ.
The software is licensed as below with The MIT License.</h6>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>
【问题讨论】:
-
您想要一个包含 3 列的表格网格,打印来自 json 的数据并克隆这一行以附加并显示剩余的 json 数据。
-
@ChaitanyaGhule 你的意思是在 JS 中克隆?因为在 HTML 中不起作用。
-
不完全克隆,但类似于复制第一行的 html 标记 (tr) 并在接下来的连续行中显示剩余数据@Blacksun
-
这不起作用,因为您将它们全部放在一个“col-md-4”中,您当然需要 3..
-
@Keith 我尝试在一行中创建 3 个不同的 md-4。此外,在多行中。
标签: javascript css json twitter-bootstrap