jquery 读取json文件
json文件格式 menu.json 文件目录js/menu.json
{
"active": "产品",
"next":"解决方案"
}
JS代码获取 使用jquery getJSON获取JSON对象 ,然后用$.each()遍历获取,
$.each(json,function(i,n){...})里面的回调函数i和n参数代表的是,元素索引和元素的值,这里要获取上面文件格式固定的熟悉,比如active使用json.active
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery-1.7.1.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
/*设置json文件里的值*/
/*获取json文件里的值*/
$.getJSON('js/menu.json',function(json){
/*遍历JSON对象, 元素索引和内容*/
$.each(json,function(i,n){
console.log("index="+i+";"+"value="+n);
$('#text').html(json.active);
/*这里修改json 并没有写入到json文件里*/
$('#text1').html(json['active']="技术优势");
alert(json.active)
})
});
</script>
<div id="text"></div><br>
<div id="text1"></div>
</body>
</html>
两个文件格式

浙公网安备 33010602011771号