繁体   English   中英

将外部json数据附加到div类别明智的

[英]Append external json data to div category wise

我不知道如何将外部json数据附加到div。 我知道追加表。但是 ,我对div。感到困惑,请帮助我解决这个疑问。 因为,它需要添加具有选定类别div的数据。

div1类别[书籍]

<div class="col-xs-12 col-sm-5 col-md-3 col-lg-2 card">
                <!--Card content-->
                <div class="card-body">
                    <!--Title-->
                    <h4 class="card-title">Card title</h4>
                    <!--Text-->
                    <img class="img-fluid z-depth-3 rounded-circle" src="https:/goo.gl/4cPCdn"
                        alt="Card image cap">
<h4 class="card-title">Category</h4>
                    <!--Card content-->
                    <a href="www.gogle.com" class="btn btn-primary">Button</a>
                </div>
            </div>

div 2类[游戏]

<div class="col-xs-12 col-sm-5 col-md-3 col-lg-2 card">
                <!--Card content-->
                <div class="card-body">
                    <!--Title-->
                    <h4 class="card-title">Card title</h4>
                    <!--Text-->
                    <img class="img-fluid z-depth-3 rounded-circle" src="https:/goo.gl/4cPCdn"
                        alt="Card image cap">
<h4 class="card-title">Category</h4>
                    <!--Card content-->
                    <a href="www.gogle.com" class="btn btn-primary">Button</a>
                </div>
            </div>

JSON

   {
    "category": {
        "books": [
            {"title":"Sentra", "url":"https:/goo.gl/4cPCdn","button":"https:google.in"},
            {"title":"Maxima", "url":"https:/goo.gl/4cPCdn"},"button":"https:google.in"}
        ],
    "games": [
            {"title":"Taurus", "url":https:/goo.gl/4cPCdn},"button":"https:/google.in"}
            {"title":"Escort", "url":https:/goo.gl/4cPCdn},"button":"https:/google.in"}
        ]
    }
}

Javascript和jQuery

<script>
        $.ajax({

            url: 'json-data.json',
            type: "get",
            dataType: "json",

            success: function (data) {
                drawTable(data);
            }
        });

        function drawTable(data) {
            for (var i = 0; i < data.length; i++) {
                drawRow(data[i]);
            }
        }

        function drawRow(rowData) {
            **This part i dont know please teach me**
        }
    </script>

小提琴

小提琴代码点击编辑

这不是完美的方法,但它应该为您提供一些帮助,帮助您了解如何通过jQuery附加到div。 您的数据被弄乱了,因此我将其修复并使其成为有效的对象。 您可能需要使用之类的东西的JSLint之前测试你的网页来检查您的数据- https://jsonlint.com/

 var data = { "category": { "books": [ {"title":"Sentra", "url":"https:/goo.gl/4cPCdn", "button":"https:google.in"}, {"title":"Maxima", "url":"https:/goo.gl/4cPCdn", "button":"https:google.in"} ], "games": [ {"title":"Taurus", "url":"https:/goo.gl/4cPCdn","button":"https:/google.in"}, {"title":"Escort", "url":"https:/goo.gl/4cPCdn","button":"https:/google.in"} ] } } /* not needed for test, we've already included our data above $.ajax({ url: 'json-data.json', type: "get", dataType: "json", success: function (data) { drawTable(data); } }); */ data = data.category; drawTable(data) function drawTable(theData) { for (category in theData) { console.log('category is '+category) var categoryEntries = theData[category] for (var i = 0; i < categoryEntries.length; i++) { var rowData = categoryEntries[i]; drawRow(category,rowData) } } } function drawRow(category,rowData) { // console.log(JSON.stringify(rowData)) var title = rowData.title; var url = rowData.url; var button = rowData.button var newDiv = '<div class="card">'+ '<div class="card-body">'+ '<h4 class="card-title">'+title+'</h4>'+ '<img class="img-fluid z-depth-3 rounded-circle" src="'+url+'" alt="Card image cap">'+ '<h4 class="card-title">'+category+'</h4>'+ '<a href="'+button+'" class="btn btn-primary">Button</a>'+ '</div>'+ '</div>' $('#'+category).append(newDiv); } 
 #books,#games { display:block; width: 100%; clear:both; } .card { float:left; width: 30vw; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <div id="books"> </div> <div id="games"> </div> 

暂无
暂无

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

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