【问题标题】:JSON date not showing in HTMLJSON 日期未在 HTML 中显示
【发布时间】:2015-07-10 19:48:01
【问题描述】:

我对 JSON 数据有疑问:我想通过 JSON URL 制作新闻提要,但信息未显示在 HTML 中。

$(function() {

var entries = [];
var dmJSON = "http://www.stellarbiotechnologies.com/media/press-releases/json";
$.getJSON( dmJSON, function(data) {

      var html = '<div class="panel-body"><h3 class="lead">${title}</h3>';

      html += '<time datetime="${published}">';
      html += '<span class="glyphicon glyphicon-time"></span> ';
      html += '<span class="month">${monthName}</span> ';
      html += '<span class="day">${day}</span>, ';
      html += '<span class="year">${year}</span></time></div>';
 $('#ticker').html

    },
&lt;div id="ticker"&gt;&lt;/div&gt;

【问题讨论】:

  • ${title}? ${published}?你在使用某种模板库吗?
  • $('#ticker').html... 然后呢?
  • 您将所有内容附加到html var,但实际上并未使用它。也许你想要$('#ticker').html(html) 而你实际上并没有遍历返回的data
  • 感谢您的回复,${title} insiad json,其实我不知道我是怎么编程的,我只需要一个他让代码正确的人。

标签: javascript html json


【解决方案1】:

你可以这样做:

<script>
 $(document).ready(function() {

    var dmJSON = "http://www.stellarbiotechnologies.com/media/press-releases/json";
    $.getJSON( dmJSON, function(data) {

          var html = '';

            // loop through all the news items, and append the 
            // title and published date to the html variable.

            for(var i = 0; i < data.news.length; i++){

                html += '<div>';
                html += data.news[i].title 
                html += '<br>';
                html += data.news[i].published 
                html += '</div>';
            }

            // append all the html variable to the ticker div.
            $("#ticker").append(html);
        });

 });
</script>

这将遍历news 节点,并获取titlepublished 日期,然后将它们附加到页面(tickerid 元素。

【讨论】:

  • thankkkkkkkkkkkkkkkkk 你干得真棒 :) 一个爱人
猜你喜欢
  • 2012-04-18
  • 2020-02-15
  • 2013-12-22
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
  • 2017-09-10
相关资源
最近更新 更多