简体   繁体   English

jQuery的:如何添加 <li> 在现有的 <ul> 与json数据

[英]Jquery : how to add <li> in an existing <ul> with json data

I have code that looks like this: 我有看起来像这样的代码:

<div id="tags">
  <ul>
  </ul>
</div>

and I'd like to append some items to the list using json data 我想使用json数据将一些项目添加到列表中

 $.getJSON("data.json", function (data) {
            var html = '';
            var len = data.tags.length;
            for (var i = 0; i < len; i++) {
                html += '<li><a href="#">' + data.tags[i].tag + '</a></li>';
            }

            $("#tags ul").append(html);


        })
        .done(function () {
            console.log("second success");
           })
       .fail(function(err){console.log(err)});

but nothing happens , where is my mistake? 但是什么也没发生,我的错误在哪里?

Your Approach 您的方法

Your code will work fine as long as the JSON structure in your data.json is in the following format - 只要data.json中的JSON结构采用以下格式,您的代码就可以正常工作-

{
"tags": [
    {
        "tag": "Tag1"
    },
    {
        "tag": "Tag2"
    },
    {
        "tag": "Tag3"
    },
    {
        "tag": "Tag4"
    }
]
}

Suggested Approach 建议的方法

If your JSON is in this structure - 如果您的JSON采用这种结构-

{
"tags": [
    "Tag1",
    "Tag2",
    "Tag3",
    "Tag4"
]
}

The javascript to make it work would be - 使它起作用的JavaScript是-

$(document).ready(function(){
    $.getJSON( "data.json", function( data ) {
        var items = '';
        $.each( data.tags, function( key, val ) {
            items += '<li id="' + key + '"><a href="#">' + val + '</a></li>';
        });
        $("#tags ul").append(items);
    }).done(function () {
        console.log("Success");
    }).fail(function(err){
        console.log(err)
    });
});

This will generate the following HTML in your DOM - 这将在您的DOM中生成以下HTML-

<div id="tags">
    <ul>
        <li id="0"><a href="#">Tag1</a></li>
        <li id="1"><a href="#">Tag2</a></li>
        <li id="2"><a href="#">Tag3</a></li>
        <li id="3"><a href="#">Tag4</a></li>
    </ul>
</div>

working fiddle with test json 与测试json一起工作

http://jsfiddle.net/BWM2E/1/ http://jsfiddle.net/BWM2E/1/

$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20%2a%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D%27WRC%27&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback", function (data) {

    var html = '';
            var len = data.query.count;

    for (var i = 0; i < len; i++) {
                html += '<li><a href="#">' + data.query.created + '</a></li>';
            }

            $("#tags ul").append(html);


        })


        .done(function () {
            console.log("second success");
           })
       .fail(function(err){console.log(err)});

in for loop I print always created property because there is only one query element 在for循环中,我打印始终创建的属性,因为只有一个查询元素

the part of the json that I have treated is the following 我已经处理过的json部分如下

{"query":{"count":1,"created":"2014-05-26T22:23:03Z"}}

in your case the problem is the url of json that make a redirect. 在您的情况下,问题是进行重定向的json的网址。

test it in this (then click on "make a request") site and see this message error that explain you the problem 站点(然后单击“发出请求”)中对其进行测试,然后查看此消息错误,向您解释问题

We weren't able to successfully make a request to http://myjtest.altervista.org/tagcloud/data.json . 我们无法成功向http://myjtest.altervista.org/tagcloud/data.json发出请求。

This is likely because of a Cross-site HTTP request and your server not having the appropriate Access-Control-Allow-Origin and Access-Control-Allow-Methods headers. 这可能是因为跨站点HTTP请求,并且您的服务器没有适当的Access-Control-Allow-Origin和Access-Control-Allow-Methods标头。

Looks like your json file is invalid. 看起来您的json文件无效。
Maybe you use single quotes instead of double quotes or something else. 也许您使用单引号而不是双引号或其他方式。 Check your data.json file. 检查您的data.json文件。
According to your js code, your file data.json should be like this: 根据您的js代码,您的文件data.json应该如下所示:

{
    "tags":[
        {"tag":"name1"},
        {"tag":"name2"},
        {"tag":"name3"}
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM