[JavaScript] 使用 jQuery 解析JSON格式数据 →→→→→进入此内容的聊天室

来自 , 2020-04-01, 写在 JavaScript, 查看 151 次.
URL http://www.code666.cn/view/c0e8517b
  1. function parseJson()
  2. {
  3.     //Start by calling the json object, I will be using a
  4.     //file from my own site for the tutorial
  5.     //Then we declare a callback function to process the data
  6.     $.getJSON('hcj.json', getPosts);
  7.    
  8.     //The process function, I am going to get the title,
  9.     //url and excerpt for 5 latest posts
  10.     function getPosts(data)
  11.     {
  12.         //Start a for loop with a limit of 5
  13.         for (var i = 0; i < 5; i++)
  14.         {
  15.             var strPost = '<h2>'
  16.                  + data.posts[i].title
  17.                  + '</h2>'
  18.                  + '<p>'
  19.                  + data.posts[i].excerpt
  20.                  + '</p>'
  21.                  + '<a href="'
  22.                  + data.posts[i].url
  23.                  + '" title="Read '
  24.                  + data.posts[i].title
  25.                  + '">Read ></a>';
  26.             //Append the body with the string
  27.             $('body').append(strPost);
  28.         }
  29.     }
  30. }
  31.  
  32. //Fire off the function in your document ready
  33. $(document).ready(function ()
  34. {
  35.     parseJson();
  36. }
  37. );
  38.  

回复 "使用 jQuery 解析JSON格式数据"

这儿你可以回复上面这条便签

captcha