如何使用jquery DOM选择器显示JSON数据?

时间:2017-06-15 07:50:14

标签: javascript jquery json

我想在我的网站上显示每条新闻的json.articles[i].title这里是我的网站截图和我的代码:

在main.js中:

(function() {

    $.getJSON('https://newsapi.org/v1/articles?source=techcrunch&sortBy=top&apiKey=my-api-key', function(json) {
        $("#sidebar-wrapper li").each(function() {
            $('li').html(json.articles[0].titles)
        });
    });
})();

我的HTML文件:

<div id="sidebar-wrapper">
    <ul class="sidebar-nav">
        <strong>Latest Headines</strong>
        <li>
            <a href="">Your news title</a>
        </li>
        <li>
            <a href="">Your news title</a>
        </li>
        <li>
            <a href="">Your news title</a>
        </li>
        <li>
            <a href="">Your news title</a>
        </li>
        <li>
            <a href="">Your news title</a>
        </li>
    </ul>
</div>

以下是我的网站截图,我希望显示json.articles[].title中的每个新闻标题,而不是“您的新闻标题”。(有关说明,请参阅屏幕截图和HTML代码)。

enter image description here

4 个答案:

答案 0 :(得分:2)

尝试以下代码

&#13;
&#13;
    (function () {

        $.getJSON('https://newsapi.org/v1/articles?source=techcrunch&sortBy=top&apiKey=045089075bc74354be01b34f6335d32b', function (json) {
            var html = "";
            $(json.articles).each(function (index, value) {
                $(".sidebar-nav").append("<li><a href='"+value.url+"'>" + value.title + "</a></li>");
            });
            

        });
    })();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="sidebar-wrapper">
                    <ul class="sidebar-nav">
                    </ul>
 </div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

如果你想要的只是迭代文章,你可以使用迭代器索引并继续增加它。这样的事情。

(function(){
    var i = 0;
    $.getJSON('https://newsapi.org/v1/articles?source=techcrunch&sortBy=top&apiKey=045089075bc74354be01b34f6335d32b',function(json) {
            $("#sidebar-wrapper li").each(function(){
            $('li').html(json.articles[i++].titles)
        });
        });
})();

但是你最好迭代文章对象并动态创建列表。

答案 2 :(得分:1)

您应该动态创建LI元素,因为JSON响应中的文章可能会有所不同。

创建元素使用jQuery(html)

//Cache the ul element
var ul = $("#sidebar-wrapper ul.sidebar-nav");
//iterate the articles
$.each(json.articles, function(index, article) {
  //Create anchor
  var anchor = $('<a>', {
    href: article.url,
    text: article.title
  });
  //Create LI and append anchor after append the LI to UL
  $('<li>').append(anchor).appendTo(ul);
});

(function() {

  $.getJSON('https://newsapi.org/v1/articles?source=techcrunch&sortBy=top&apiKey=045089075bc74354be01b34f6335d32b', function(json) {
    var ul = $("#sidebar-wrapper ul.sidebar-nav");
    $.each(json.articles, function(index, article) {
      var anchor = $('<a>', {
        href: article.url,
        text: article.title
      });
      $('<li>').append(anchor).appendTo(ul);
    });
  });
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="sidebar-wrapper">
  <strong>Latest Headines</strong>
  <ul class="sidebar-nav">
  </ul>
</div>

答案 3 :(得分:1)

您还可以使用班级

更新li数据

试一下

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){
    $.getJSON('https://newsapi.org/v1/articles?source=techcrunch&sortBy=top&apiKey=045089075bc74354be01b34f6335d32b',function(json) {
        var x=document.getElementsByClassName("title");
        for(var i=0;i<x.length;i++)
            x[i].innerHTML = json.articles[0].titles;
    }); }); </script>

<div id="sidebar-wrapper">
                    <ul class="sidebar-nav">
                            <strong>Latest Headines</strong>
                        <li>
                            <a href="" class="title">Your news title</a>
                        </li>
                        <li>
                            <a href="" class="title">Your news title</a>
                        </li>
                        <li>
                            <a href="" class="title">Your news title</a>
                        </li>
                        <li>
                            <a href="" class="title">Your news title</a>
                        </li>
                        <li>
                            <a href="" class="title">Your news title</a>
                        </li>
                    </ul>  </div>