如何正确加载JSON到Bootstrap选项卡

9rbhqvlz  于 2023-07-01  发布在  Bootstrap
关注(0)|答案(2)|浏览(144)

我试图加载JSON数据到Bootstrap 3标签动态像下面的代码。我能够加载导航标签,但没有任何成功的加载内容。我该怎么解决?

var data = {
        "action":
            [
                { "id": "1001", "name": "Matrix" },
                { "id": "1002", "name": "IP Man" },
                { "id": "1003", "name": "Revenge" }
            ],
        "comedy":
            [
                { "id": "2001", "type": "Iceman" },
                { "id": "2002", "type": "Pat & Mat" },
                { "id": "2003", "type": "Sugar" }
                 ],
        "animation":
            [
                { "id": "3001", "type": "Frozen" },
                { "id": "3002", "type": "Tangled" },
                { "id": "3003", "type": "Croods" }
                 ]
    
    };
    
    for (var i in data) {
      $('.nav-tabs').append('<li role="presentation" class=""><a href="#' + i + '" aria-controls="' + i + '" role="tab" data-toggle="tab">' + i + '</a></li>');
    
    }
      for (var j = 0; j < data[i].length; j++) {
        var obj = data[i][j];
        $('.tab-content').append('<div role="tabpanel" class="tab-pane" id="' + obj.name + '">' + obj.name + '</div>');
     }

$('.nav-tabs li').eq(0).addClass('active');
body{padding:30px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="container">
  <ul class="nav nav-tabs" role="tablist"></ul>
  <div class="tab-content"></div>
</div>
q7solyqu

q7solyqu1#

您的代码中有几个逻辑错误和打字错误。下面是工作版本。
错别字:在“action”中你带参数名,但在其他两个参数名是一个类型。
逻辑:1.使用.append导致关闭HTML元素,因此需要构建一个完整的字符串,然后追加。2.在第二次循环后,你必须关闭父for循环,否则,你总是只有一个标签。

var data = {
        "action":
            [
                { "id": "1001", "type": "Matrix" },
                { "id": "1002", "type": "IP Man" },
                { "id": "1003", "type": "Revenge" }
            ],
        "comedy":
            [
                { "id": "2001", "type": "Iceman" },
                { "id": "2002", "type": "Pat & Mat" },
                { "id": "2003", "type": "Sugar" }
                 ],
        "animation":
            [
                { "id": "3001", "type": "Frozen" },
                { "id": "3002", "type": "Tangled" },
                { "id": "3003", "type": "Croods" }
                 ]
    
    };
    
    for (var i in data) {
      $('.nav-tabs').append('<li role="presentation" class=""><a href="#' + i + '" aria-controls="' + i + '" role="tab" data-toggle="tab">' + i + '</a></li>');
    
      var div = '<div role="tabpanel" class="tab-pane" id="' + i + '">';
      
      for (var j = 0; j < data[i].length; j++) {
        var obj = data[i][j];
        div += '<div id="' + obj.id + '">' + obj.type + '</div>';
     }
     $('.tab-content').append(div);
}

$('.nav-tabs li').eq(0).addClass('active');
$('.tab-content div').eq(0).addClass('active');
body{padding:30px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="container">
  <ul class="nav nav-tabs" role="tablist"></ul>
  <div class="tab-content"></div>
</div>
iyzzxitl

iyzzxitl2#

var data = {
        "action":
            [
                { "id": "1001", "type": "Matrix" },
                { "id": "1002", "type": "IP Man" },
                { "id": "1003", "type": "Revenge" }
            ],
        "comedy":
            [
                { "id": "2001", "type": "Iceman" },
                { "id": "2002", "type": "Pat & Mat" },
                { "id": "2003", "type": "Sugar" }
                 ],
        "animation":
            [
                { "id": "3001", "type": "Frozen" },
                { "id": "3002", "type": "Tangled" },
                { "id": "3003", "type": "Croods" }
                 ]
    
    };
    
    for (var i in data) {
      $('.nav-tabs').append('<li role="presentation" class=""><a href="#' + i + '" aria-controls="' + i + '" role="tab" data-toggle="tab">' + i + '</a></li>');
    
      var div = '<div role="tabpanel" class="tab-pane" id="' + i + '">';
      
      for (var j = 0; j < data[i].length; j++) {
        var obj = data[i][j];
        div += '<div id="' + obj.id + '">' + obj.type + '</div>';
     }
     $('.tab-content').append(div);
}

$('.nav-tabs li').eq(0).addClass('active');
$('.tab-content div').eq(0).addClass('active');
body{padding:30px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="container">
  <ul class="nav nav-tabs" role="tablist"></ul>
  <div class="tab-content"></div>
</div>

相关问题