[英]How do I load the elements of an array inside of an object in JSON?
I asked a similar (but different) question before. 我之前问过类似(但不同)的问题 。
I've since changed how I'm approaching the problem. 此后,我改变了解决问题的方式。
This is my JSON file. 这是我的JSON文件。
Here's my javascript so far: 到目前为止,这是我的JavaScript:
$(document).ready(function () {
var loadArticle = function (articleID, articleH) {
var article = $("#article").clone();
var thelist = '<dl id="article' + articleID + 'sections"></dl>';
$(article).attr("id", "article" + articleID);
$(article).append(articleH);
$(article).html(thelist);
return article;
}
var loadSection = function (articleID, sectionID, sectionContent) {
var section = "#article" + articleID + "sections";
$(section).append("<dt>Section " + sectionID + "</dt>");
$(section).append("<dd>" + sectionContent + "</dd>");
return section;
}
var loadConstitution = function (d) {
$.each(d, function (i) {
var articleID = i + 1;
var articleH = "<p class='lead' id='article" + articleID + "'><strong>Article " + articleID + "</strong></p>";
$("#constitutionHolder").append(loadArticle(articleID, articleH));
//$.each(d.sections, function(j){
// var sectionID = j + 1;
// var theSection = d.sections[j];
//$("#article" + articleID + "sections").append(articleID, sectionID, mySection);
//})
})
}
$.getJSON('data/stuff.json', loadConstitution);
})
Basically, I go through each data point and make a new <div>
with the id="article1"
(or whatever the number of the article may be). 基本上,我遍历每个数据点并使用
id="article1"
(或任何文章编号)创建一个新的<div>
。 Then, since each article object is filled with one array of "sections"
, I need to access the data in this array and append it to the <dl></dl>
I have. 然后,由于每个article对象都填充有一个
"sections"
数组,因此我需要访问该数组中的数据并将其附加到我拥有的<dl></dl>
中。
In my HTML, this is what I'm .clone()
ing: 在我的HTML中,这就是我的
.clone()
:
<div id="constitutionHolder">
<div class="alert text-black" id="article">
<p class="lead" id="articleHeader"><strong>Article 1</strong>
</p>
<dl id="sectionList"> <dt id="article_section">Section 1</dt>
<dd id="article_sectionINFO">this stuff belongs to section 1</dd>
</dl>
</div>
</div>
I have 2 problems so far: 到目前为止,我有2个问题:
$(article).append(articleH);
isn't working 没用
I can't load the sections into sectionList
. 我无法将这些部分加载到
sectionList
。
How do I fix this? 我该如何解决?
There are lots of ways to approach this, but here's an example that you might be able to modify to suit your application: http://jsfiddle.net/DgU3w/ . 有很多方法可以解决此问题,但以下示例可以根据您的应用程序进行修改: http : //jsfiddle.net/DgU3w/ 。 (Note that I stripped out some details to try to understand the problem)
(请注意,我删除了一些详细信息以尝试理解问题)
A few things I noticed in your example: 我在您的示例中注意到了一些事情:
loadArticle()
clobbers some work with this statement: $(article).html(thelist);
loadArticle()
使此语句变得有些有用: $(article).html(thelist);
loadArticle()
clones a block of HTML, but then it appends new elements that match cloned ones. loadArticle()
克隆HTML块,但随后会添加与克隆的元素匹配的新元素。 $(jQueryObj)
), but it's not necessary either. $(jQueryObj)
)并没有什么坏处,但是也没有必要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.