【问题标题】:How to output data as HTML from JSON object using getJSON如何使用 getJSON 从 JSON 对象将数据输出为 HTML
【发布时间】:2012-07-17 17:39:34
【问题描述】:

你好,我会尽量保持简单和简短

我有一个每 5 秒运行一次的 getJSON 函数。现在,当我使用 document.write 函数显示数据时,它似乎想要更新。页面卡在加载循环中。如何让数据显示在我的页面上?我的 JSON 很好,但无论如何我都会给你看。

  <script type="text/javascript">

 $.ajaxSetup ({  
    cache: false  
     });  

    setInterval(function(){ $.getJSON('names.json', function(data) {

    for(i in data)  {

        document.write(data[i] + "<br/>");
    }

  });



},5000);

 </script>

这是 JSON 对象

{
  "one": "",
  "two": "Beady little eyes",
  "three": "Little birds pitch by my doorstep"
}

【问题讨论】:

    标签: javascript html ajax json setinterval


    【解决方案1】:

    实际上不要使用document.write。加载页面后,将删除该页面。使用(jQuery 的)DOM 方法来操作 DOM。

    $.getJSON('names.json', function(data){
        for(var i in data){
            $('#myDiv').append(data[i]);
        }
    });
    

    【讨论】:

      【解决方案2】:

      我会推荐你​​使用 jQuery,

      我用它从我的 json 项目中创建了一个表单,我希望这会有所帮助...

      function jsonFormCreator(frmJSON)
      {
          var createdHTML = ""; var elementType, id;
          console.log(JSON.stringify(frmJSON));
      
      
      
          for(item in frmJSON)
          {
              formElement = frmJSON[item];    elementType = formElement.elementType;  id = formElement.id;
      
              if(elementType == "input")
              {
                  createdHTML += "<input id='" + id +"'";
                  if(formElement.type == "checkbox") 
                  {createdHTML += " type='" + formElement.type + "' checked='" + formElement.checked + "'";}
                  else{createdHTML += "type='" + formElement.type + "' value='" + formElement.value + "' onclick='" + formElement.onclick + "'";}
                  createdHTML += "/><br>"
              }
              else if(elementType == "textarea")
              {createdHTML += "<textarea id='" + formElement.id  + "' rows='" + formElement.rows +  "' cols='" + formElement.cols + "' value='" + formElement.value + "'></textarea><br>";}
              else if(elementType == "select")
              {
                  var options = formElement.values;
                  createdHTML += "<select id='" + formElement.id+ "'>";
                      for(i = 0 ; i < options.length ; i++)
                      {createdHTML += "<option value='" + options[i][0] + "'>" + options[i][1] + "</option>";} //Adding each option
                  createdHTML+= "</select><br>";
              }
          }
          console.log("Complete");console.log(createdHTML);
          document.getElementById('mainContainer').innerHTML = createdHTML;//Adding to the DOM
      }
      

      我的 JSON 看起来像这样

      {
          "0": {
              "elementType": "input",
              "id": "frm1",
              "type": "text",
              "value": "form Liam",
              "label": "Test Text Input"
          },
              "itemBTN": {
              "elementType": "input",
              "id": "frmAlert",
              "type": "button",
              "onclick" : "loader(homePage);",
              "value": "Home Page",
              "label": ""
          },
          "item2": {
              "elementType": "textarea",
              "id": "frm2",
              "rows": 5,
              "cols": 30,
              "value": "helddddddddlo",
              "label": "Test Textarea"
          },
          "item3": {
              "elementType": "select",
              "id": "frm3",
              "values": [
                  [
                      "value1",
                      "Pick Me"
                  ],
                  [
                      "value2",
                      "UserDis2"
                  ],
                  [
                      "value3",
                      "UserDis3"
                  ],
                  [
                      "value4",
                      "UserDis4"
                  ],
                  [
                      "value5",
                      "UserDis5"
                  ],
                  [
                      "value6",
                      "UserDis6"
                  ]
              ],
              "label": "Test Select"
          },
          "item4": {
              "elementType": "input",
              "id": "frm4",
              "label": "Checkbox",
              "type": "checkbox",
              "checked": true
          }
      }
      

      这段代码将表单添加到我的 div 标签中,id 为 mainContainer 我知道它的很多代码,但我希望它有所帮助!

      【讨论】:

        【解决方案3】:

        你想渲染包含数据的 dom,然后当你得到数据时更新 dom。

        作为一个非常简单的例子,在你的页面上有一个容器

        <div id="one"></div>
        

        然后在您的 ajax 成功处理程序中

        $("#one").text(json.one);
        

        这使用 jquery 来获取 id 为“one”的 dom 元素,并更新其文本。

        【讨论】:

          猜你喜欢
          • 2020-12-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-02
          • 1970-01-01
          • 2016-09-30
          相关资源
          最近更新 更多