4

我正在尝试以Bullets的形式显示特定的 json 数据。将每资料放入<li>,将每篇标题放入<p>,并使那些标题成为一个link。最后,考虑一下点击标题显示相关内容 的索引<div>。我已经在下面有一些代码(还没有工作)。

网页

<div id="page1">
    <ul id="courses"></ul>
</div>
<div id="page2">
    <p id="content"></p>
</div>

JS代码

var jsonString = '[{"chapter":"General","title":"News forum","content":"Text1"},
{"chapter":"CHAPTER 1","title":"1.1 Introduction","content":"Text2"},
{"chapter":"CHAPTER 1","title":"1.2 Main Idea","content":"Text3"},
{"chapter":"CHAPTER 2","title":"2.1 Architecture","content":"Text4"},
{"chapter":"CHAPTER 3","title":"3.1 Liter.overview","content":"Text5"}]';

var myData = JSON.parse(jsonString);

$(document).ready(function() {
    var $clist = $('#courses');
    for(var i in myData) {
        $('<li><h3>' +this.[i].chapter+ '</h3><p><a href="#page2" onclick="dContent(i)">' +this.title+ '</a></p></li>').appendTo($clist);
    }

    function dContent() {
        var $ccontent = $('#content');
        $(this.[i].content).appendTo($ccontent);
    }
});

预期结果:

- General
    News forum          // onclick display 'Text1' in <p id="content">
- CHAPTER 1
    1.1 Introduction    // onclick display 'Text2' in <p id="content">
    1.2 Main Idea       // onclick display 'Text3' in <p id="content">
- CHAPTER 2
    2.1 Architecture    // onclick display 'Text4' in <p id="content">
- CHAPTER 3
    3.1 Liter.overview  // onclick display 'Text5' in <p id="content">

任何帮助,将不胜感激。

更新:这是JSFIDDLE项目。

4

5 回答 5

2
var jsonString = '[{"chapter":"General","title":"News forum","content":"Text1"},{"chapter":"CHAPTER 1","title":"1.1 Introduction","content":"Text2"},{"chapter":"CHAPTER 1","title":"1.2 Main Idea","content":"Text3"},{"chapter":"CHAPTER 2","title":"2.1 Architecture","content":"Text4"},{"chapter":"CHAPTER 3","title":"3.1 Liter.overview","content":"Text5"}]';
var myData = JSON.parse(jsonString);

var dContent = function(event) {
    $ccontent.html($(this).data('content'));
}

var $clist = $('#courses');
var $ccontent = $("#content");
var html = '';
var chapterList = [];

$clist.on('click', 'li', dContent);

$.each(myData, function(index, item) {
    if ($.inArray(item.chapter, chapterList) === -1) {
        chapterList.push(item.chapter);
        html += '<li data-content="'+ item.content +'"><h3>' + item.chapter + '</h3><p><a href="#page2">' + item.title + '</a></p></li>';
    }
    else {
        html += '<li data-content="'+ item.content +'"><p><a href="#page2">' + item.title + '</a></p></li>'
    }
});
$clist.html(html);
于 2013-02-21T20:20:59.013 回答
1

我已经编写了一个脚本来执行此操作,包括将同一章节中的项目放在一起。你可以在这里看到一个演示小提琴

我大部分都使用原生 JavaScript,除了 jQuery$(a).on('click', ..$(document).ready确保兼容性。为什么这么长?因为我<ul>用 DOM 方法构建了,而不是 html 字符串。这使得缓存和附加元素变得容易。最后,通过生成器函数添加内容。我这样做的方式意味着页面将使用更多的内存,但你可以拥有在内容部分显示任何在 JavaScript 中有效的字符串。您可能希望对其进行样式设置whitespace: pre-wrap;以按预期显示新行。

无论如何,这是代码

var jsonString = '[{"chapter":"General","title":"News forum","content":"Text1"},\
{"chapter":"CHAPTER 1","title":"1.1 Introduction","content":"Text2"},\
{"chapter":"CHAPTER 1","title":"1.2 Main Idea","content":"Text3"},\
{"chapter":"CHAPTER 2","title":"2.1 Architecture","content":"Text4"},\
{"chapter":"CHAPTER 3","title":"3.1 Liter.overview","content":"Text5"}]';
// the \ at line ends is to escape the new line in the string literal

var myData = JSON.parse(jsonString);

$(document).ready(function() {
    var courses_ul = document.getElementById('courses'), // cache elements
        content_elm = document.getElementById('content'),
        i, li, h3, p, a, // vars for loop
        chapters = {}, chap; // cache chapters
    for (i = 0; i < myData.length; ++i) {
        chap = myData[i].chapter; // shorthand since we'll use it a few times
        // make <p>, <a>
        p = document.createElement('p');
        a = document.createElement('a'); // could append <a> to <p> here if you want
        a.setAttribute('href', '#page2');
        a.appendChild(document.createTextNode(myData[i].title));
        // set up click
        $(a).on('click', (function (content) { // generator - will give scope to
            return function () { // this returned event listener.
                content_elm.innerHTML = '';
                content_elm.appendChild(document.createTextNode(content));
            };
        }(myData[i].content))); // chose `content` not `i` so no reliance on `myData`
        // now check cache if chapter exists -
        if (chap in chapters) { // retreive <li> for chapter from cache
            li = chapters[chap]; // from cache
            // append <p>, <a>
            li.appendChild(p).appendChild(a);
        } else { // if not in cache
            li = document.createElement('li'); // make a new <li>
            chapters[chap] = li; // and cache
            // make & append <h3>
            h3 = document.createElement('h3');
            h3.appendChild(document.createTextNode(chap));
            li.appendChild(h3);
            // append <p>, <a> and to <ul>
            courses_ul.appendChild(li).appendChild(p).appendChild(a);
        }
    }
});
于 2013-02-22T12:24:42.303 回答
1

您的 JSON 结构无效。正确的结构如下:

[
    {
        "chapter": "General",
        "title": "News forum",
        "content": "Text1"
    },
    {
        "chapter": "CHAPTER 1",
        "title": "1.1 Introduction",
        "content": "Text2"
    },
    {
        "chapter": "CHAPTER 1",
        "title": "1.2 Main Idea",
        "content": "Text3"
    },
    {
        "chapter": "CHAPTER 2",
        "title": "2.1 Architecture",
        "content": "Text4"
    },
    {
        "chapter": "CHAPTER 3",
        "title": "3.1 Liter.overview",
        "content": "Text5"
    }
]

请注意,3.1 Liter.overview","content":"Text5"},您的 JSON 结构中的逗号在这里,它在这里失败

用代码更新答案

var jsonString = '[{"chapter": "General","title": "News forum","content": "Text1"},{"chapter": "CHAPTER 1","title": "1.1 Introduction","content": "Text2"},{"chapter": "CHAPTER 1","title": "1.2 Main Idea", "content": "Text3"},{"chapter": "CHAPTER 2","title": "2.1 Architecture","content": "Text4"},{"chapter": "CHAPTER 3","title": "3.1 Liter.overview","content": "Text5"}]';

var myData = JSON.parse(jsonString);
$(document).ready(function() {
    function dContent() {
       $("#content").css("border","2px solid red").css("height","100px");
       $("#content").html($(this).data('value'));
    }
     $("#courses").on('click','li', dContent) 
    $.each(myData, function(index,item) { 
    $("#courses").append("<li class='li' data-value="+item.content+">"+item.chapter+"  <p>"+item.title+"</p></li>");

    })



});

JSFIDDLE 上的演示

于 2013-02-21T20:22:18.467 回答
0

this.[i].chapter应该是myData[i].chapter。事实上,这是一个语法错误。

然后你应该重新考虑你的其他用法this是否正确。

于 2013-02-21T23:22:54.763 回答
0

将其复制到您的 JSFiddle 并检查 jQuery,将使其工作。

var jsonString = '[{"chapter":"General","title":"News forum","content":"Text1"},{"chapter":"CHAPTER 1","title":"1.1 Introduction","content":"Text2"},{"chapter":"CHAPTER 1","title":"1.2 Main Idea","content":"Text3"},{"chapter":"CHAPTER 2","title":"2.1 Architecture","content":"Text4"},{"chapter":"CHAPTER 3","title":"3.1 Liter.overview","content":"Text5"}]';

var myData = JSON.parse(jsonString);

$(document).ready(function() {
    var $clist = $('#courses');

    $.each(myData, function(i,o){
        $('<li><h3>' +o.chapter+ '</h3><p>' +
          '<a href="#page2" onclick="dContent(\''+o.content+'\')">' +
          o.title + '</a></p></li>').appendTo($clist);
    });

    window.dContent = function(content) {
        var $ccontent = $('#content');
        $ccontent.append(content);

    } 
});
于 2013-02-21T23:39:02.350 回答