<ol id="author_list">
<li id="author_8"><div class="author">Author 1</div>
<ol>
<li class="affiliation_7"><div class="affiliation">School1</div>
</li>
<li class="affiliation_8"><div class="affiliation">School2</div>
</li>
</ol>
</li>
<li id="author_10"><div class="author">Author 3</div>
<ol>
<li class="affiliation_7"><div class="affiliation">School1</div>
</li>
</ol>
</li>
<li id="author_9"><div class="author">Author 2</div>
<ol>
<li class="affiliation_8"><div class="affiliation">School2</div>
</li>
</ol>
</li>
</ol>
看起来像这样
我希望能够遍历这个列表并以 json 格式存储列表的确切位置,以便以后可以以相同的顺序重现该列表。
我不确定 json 应该是什么样子,但这就是我想出的。不确定这是否也是存储数据的最佳方式..
{
"association1" : {
"author": "author_8",
"affiliation": {
"affiliation_7": "0,0",
"affiliation_8": "0,1"
},
"association2" : {
"author": "author_10",
"affiliation": {
"affiliation_7": "1,0",
},
"association3" : {
"author": "author_9",
"affiliation": {
"affiliation_8": "2,0",
},
}
到目前为止我的代码
var indexList = [];
var level2 = $('#author_list li ol li')
level2.each(function() {
var topIndex = $(this).index();
var subIndex = $(this).parent().parent().index();
indexList.push((topIndex, subIndex));
alert(indexList);
})
更新
只是为了澄清一下0,0
, ,分别0,1
指 和 的索引值,School1
依此类推School2
Author1