使用.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>
网友评论