【问题标题】:How to display a second JSON element in JavaScript?如何在 JavaScript 中显示第二个 JSON 元素?
【发布时间】:2014-09-10 08:06:40
【问题描述】:

我正在尝试设置一个时事通讯,它从一个 JSON 文件中获取其内容,其中包含许多由标题和文本组成的文章。使用 JavaScript,我希望 HTML 页面显示一个包含所有标题的列表。当用户单击其中一个标题时,应显示相应的文本(理想情况下,当他或她单击另一个标题时,应隐藏先前的文本而仅显示新的文本-但可能都是在另一个迭代中)。

脚本列出​​了标题,但到目前为止,我只能将它们制作成 HTML 链接,将用户发送到一个不存在的页面,我希望通过 innerHTML 在 div 中显示文本。我对 JavaScript 很陌生(对 jQuery 一无所知),所以如果有人能告诉我如何从 JSON 文件中获取文本元素并一次显示它们,我会很高兴。

我的 JSON 数据:

var article = [
    {
        "title": "This is title no. 1",
        "text": "Here is the text of the first article"
    },
    {
        "title": "This is title no. 2",
        "text": "Here is the text of the second article"
    },
    {
        "title": "This is title no. 3",
        "text": "Here is the text of the third article"

    }
]

我的 HTML:

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="newsletter.json"></script>
    <script type="text/javascript">
        function getData() {
            for(i = 0; i < article.length; i++) {
                document.getElementById("showData").innerHTML += "<li>" + article[i].title.link(article[i].text) + "</li>";
            }
        }
    </script>
</head>
<body onload="getData()">
    <div id="showData"></div>
</body>
</html>

【问题讨论】:

  • 请注意,您的问题与 JSON无关。您发布的是一个普通的 JavaScript 数组。即使您在某处以 JSON 格式获取数据,这也无关紧要,因为您的问题似乎与处理数组有关。 JSON 是一种独立于语言的文本数据交换格式。你所拥有的,var article = [...];,是 JavaScript。
  • 好吧,它看起来像一个 JSON 问题,但您已将数据显示为 javascript 数组。它是否应该从 newsletter.json 获取数据,而您刚刚给出了该数组以显示它是如何工作的?
  • @Popnoodles:既然OP提到他能够显示标题,我相信article确实包含在另一个脚本中并且包含一个数组。
  • 啊,所以你认为他的 newsletter.json 不是 json。我可以看到这是真的。
  • @Popnoodles:特别是因为它作为脚本包含在type="text/javascript" 中。如果它真的包含 JSON,它仍然会被评估为 JavaScript 并引发语法错误。 article 将不存在,其他脚本也会出错。

标签: javascript json


【解决方案1】:

使用 jQuery 很容易做到这一点。

查看这里的工作小提琴:http://jsfiddle.net/manishie/pk7264u6/

html:

<body>
    <div id="showData"></div>
</body>

javascript:

function getData() {
    var html = '';

    for(i = 0; i < article.length; i++) {
        html += "<li data-text='" + "'>" + article[i].title.link(article[i].text) + "</li>";
    }
    $('#showData').html('<ul>' + html + '</ul>');

    // Use jQuery 'on' to run code when any Anchor tag inside the #showData div is clicked.
    $('#showData').on('click', 'a', function(e) {

        // remove any previously displayed text (in divs with the .text class)
        $('#showData .text').remove();    

        // after the current element (the anchor tag that was clicked) add a new div with the class .text.  Inside that div, display the href attribute of the anchor tag that was clicked.
        $(this).after('<div class="text">' + $(this).attr('href') + '</div>');

        // disable normal anchor handling so you don't get taken to a non-existent page.
        e.preventDefault();    

    });

}

// Use jQuery $(document).ready to make sure the DOM is loaded before you run anything (http://learn.jquery.com/about-jquery/how-jquery-works/) instead of the onLoad in body.
$(document).ready(function() {
    getData();
});

【讨论】:

  • 非常感谢。我曾希望有一个 JavaScript 解决方案,但我想,虽然它看起来对我来说更熟悉,但它也会更复杂。无论如何,这似乎为 jQuery 提供了一个强有力的案例,所以我会尝试继续。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-29
  • 2020-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多