【问题标题】:Populating html divs with respective json data object使用相应的 json 数据对象填充 html div
【发布时间】:2016-01-05 07:46:59
【问题描述】:

如何使用 jquery/javascript(循环和 switch case)从外部文件(div 标签、css 类和 json 对象名称相同)填充嵌套 html div 的数量。 我对 html 和 json 结构给出了一个模糊的概念。

    div1
     div1.1
      div1.1.1
      div1.1.2
      div1.1.3

  json array
   [
    1
    {
      1.1
      {[
        1.1.1
        1.1.2
        1.1.3
        ]
       }
       }
     ]

【问题讨论】:

  • 你考虑过使用jquery吗?文档非常详尽。
  • 我发现很难为此生成一个 jquery 代码..
  • 那么你需要如何将值加载到元素中?页面加载后?
  • div1 等是什么意思?你的意思是这样吗? <div id="div1"></div>?
  • 按 div 1 与类名 1 的 div 相同

标签: javascript jquery html css json


【解决方案1】:
<script>
$(document).ready(function() {
var str = "";
//object that contain your JSON
var jsonObj;
//first iteration
$.each(jsonObj,function(i,data){
//here you will create div1
str = str + '<div1>';
    $.each(data.jsonObj1.1,function(j,data){
        str = str + '<div1.1>';
        $.each(data.jsonObj1.1,function(k,data){
        str = str + '<div1.'+k+1+'>';
        });
    });
});

});
</script>

【讨论】:

猜你喜欢
  • 2017-02-10
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
  • 2013-10-31
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
相关资源
最近更新 更多