美文网首页
循环遍历JSON数据

循环遍历JSON数据

作者: 黑夜的眸 | 来源:发表于2018-05-31 21:51 被阅读0次

使用.forEach()函数来循环遍历JSON数据写到html变量中

<script>
  $(document).ready(function() {
    $("#getMessage").on("click", function() {
      $.getJSON("/json/cats.json", function(json) {

        var html = "";
        // 请把你的代码写在这条注释以下
        json.forEach(function(val){
          var keys = Object.keys(val);
          keys.forEach(function(key){
            html += "<b>" + key + "</b>:"+val[key]+"<br>";
          });
        });
        // 请把你的代码写在这条注释以上
        $(".message").html(html);
      });
    });
  });
</script>

相关文章

网友评论

      本文标题:循环遍历JSON数据

      本文链接:https://www.haomeiwen.com/subject/kxpcsftx.html