繁体   English   中英

如何在json值内使用一些html元素标签

[英]how to use some html element tag inside the json value

我使用Ajax,js将json数据绑定到html表。 在我的项目中,我有4个表格标题(序号,名称,年份,下载链接)。 前三列同步并正常运行,但没有错误。但是,最后一列我需要带有链接的引导下载按钮。 我如何使用json做到这一点?

确切地我需要什么:如果我加载页面,则需要我的名称,年份,引导下载按钮和插入的链接。

html

<body>
<div class="container">
    <div class="first bg-primary">
        <h2>A R Rahman All Movies</h2>
        <p>Download songs free</p>
    </div>
    <!--  -->
    <table id="personDataTable" class="table table-dark table-hover">
        <thead>
            <tr>
                <th>S.No</th>
                <th>Movie Name</th>
                <th>Year</th>
                <th>Download Link</th>
            </tr>
        </thead>

    </table>
    <!-- table end -->

    <script>
        $.ajax({
            //url: 'https://jsonplaceholder.typicode.com/posts',
            url: 'https://10rs.000webhostapp.com/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) {
            var row = $("<tr />")
            $("#personDataTable").append(row);
            row.append($("<td>" + rowData.id + "</td>"));
            row.append($("<td>" + rowData.name + "</td>"));
            row.append($("<td>" + rowData.year + "</td>"));
            row.append($("<td>" + rowData.link + "</td>"));
        }
    </script>

    <footer class="bg-danger">
        <p>Design and Developed by Rajadurai</p>
    </footer>
</div>
<!-- container end -->
</body>

这是数据的输出

[
{
  "id": 1,
  "name": "Roja",
  "year": "1992",
  "link": "<a href="#" class="btn btn-info" role="button">Link Button</a>"
},
{
  "id": 2,
  "name": "Gentleman",
  "year": "1993"
},
{
  "id": 3,
  "name": "Kizhakku Cheemayile",
  "year": "1993"
},
{
  "id": 4,
  "name": "Pudhiya Mugam",
  "year": "1993"
},
{
  "id": 5,
  "name": "Thiruda Thiruda",
  "year": "1993"
}
]

你可以用这个

row.append($("<td><a href='" + rowData.link + "'><button class='btn'>Go</button></a></td>"));

您必须通过仅放置链接或用简单(')代替双破折号(“)来修改数据中的链接

1-如果您只想在数据中放入链接

//像这样修改您的代码

$.ajax({
        //url: 'https://jsonplaceholder.typicode.com/posts',
        url: 'https://10rs.000webhostapp.com/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) {
        var row = $("<tr />")
        $("#personDataTable").append(row);
        row.append($("<td>" + rowData.id + "</td>"));
        row.append($("<td>" + rowData.name + "</td>"));
        row.append($("<td>" + rowData.year + "</td>"));
        row.append($("<td><a href='" + rowData.link + "' class='btn btn-info' role='button'>Link Button</a></td>"));
    }

2-如果您想用简单的Cote(')来代替double Cote(“),则无需更改代码即可使用

最后,我得到了完整的答案,项目运行良好。

的HTML

<div class="container">
    <div class="header">
        <h2>A R Rahman All Movie Songs</h2>
        <p>Download songs free</p>
    </div>
    <!-- header end -->
    <div class="table-responsive-md">
        <table id="personDataTable" class="table table-dark">
            <thead>
                <tr>
                    <th>S.No</th>
                    <th>Movie Name</th>
                    <th>Year</th>
                    <th>Download</th>
                </tr>
            </thead>
        </table>
    </div>
    <!-- table end -->

    <footer class="bg-success">
        <p style="color:#ffffff;">Design and Developed by
            <span style="color:red;background-color:yellow;">Rajadurai</span>
        </p>
    </footer>
</div>

JAVASCRIPT

<script>
    $.ajax({
        //url: 'https://jsonplaceholder.typicode.com/posts',
        url: 'https://10rs.000webhostapp.com/ar/json-data.json',
        //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) {
        var row = $("<tr />")
        $("#personDataTable").append(row);
        row.append($("<td>" + rowData.id + "</td>"));
        row.append($("<td>" + rowData.title + "</td>"));
        row.append($("<td>" + rowData.year + "</td>"));
        row.append($("<td><a href='" + rowData.link + "' class='btn btn-info' role='button' target='_blank'>Download</a></td>"));
}
</script>

JSON格式

[{
    "id": 1,
    "title": "Roja",
    "year": "1992"
},
{
    "id": 2,
    "title": "Gentleman",
    "year": "1993"
},
{
    "id": 3,
    "title": "Kizhakku Cheemayile",
    "year": "1993"
},
{
    "id": 4,
    "title": "Pudhiya Mugam",
    "year": "1993"
},
{
    "id": 5,
    "title": "Thiruda Thiruda",
    "year": "1993"
}]

输出值

点击此链接查看输出

暂无
暂无

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

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