1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; } .root{ width: 800px; margin: 30px auto; border: 2px solid gold; } ul{ list-style: none; width: 100%; display: flex; flex-wrap: wrap;
h3{ width: 97%; height: 40px; line-height: 40px; padding-left: 3%; } li{ width: 10%; height: 40px; line-height: 40px; text-align: center; overflow: hidden; box-sizing: border-box; border:1px solid gold; } </style> </head> <body> <div class="root"> </div> </body> </html>
<script src="js/jquery-3.5.1.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript"> var request; if(window.XMLHttpRequest){ request = new XMLHttpRequest(); }else{ request = new ActiveXObject("Microsoft.XMLHTTP"); } request.open("GET", "city.json", true); request.onreadystatechange = function(){ if(request.readyState == 4 && (request.status == 200)){
getDataByJSONString(request.responseText); } } request.send(); function getDataByJSONString(jsonStr){ var bigObject = JSON.parse(jsonStr); var allKeyArray = Object.keys(bigObject); allKeyArray.sort(); for(var i in allKeyArray){ var k = allKeyArray[i]; var cityArray = bigObject[k]; var ul = document.createElement("ul"); $(".root").append(ul); var h3 = document.createElement("h3"); $(h3).html(k); $(ul).append(h3); for(var j in cityArray){ var cityObj = cityArray[j]; var li = document.createElement("li"); $(li).html(cityObj.name); $(ul).append(li); } } } </script>
|