<script type="text/javascript" src="./js/lib/jquery.js"></script> <script type="text/javascript"> $(function(){ $(‘button‘).click(function(){show();}); }); function show(){ $.getJSON("./source/student.json",function(stu){ //在“./source/student.json” //地址里找到文件,然后调用函数,stu为这个文件对象 $("#tab").empty(); //防止多次刷新出现重复值,即每次点击按钮之后会先清空表格里的内容 $(‘#tab‘).append("<tr><td>"+stu.name+"</td><td>"+stu.age+"</td></tr>"); }); } </script> </head> <body> <button>点我刷新数据</button> <table> <tr> <th>姓名</th> <th>年龄</th> </tr> </table> <table style="border-style:solid;width:150px;height:20px;" id="tab"> <tr> <td></td> <td></td> </tr> </table> </body>
student.json:
{"name":"张三","age":"15"}
结果:
<script type="text/javascript" src="./js/lib/jquery.js"></script> <script type="text/javascript"> $(function(){ $(‘button‘).click(function(){show();}); }); function show(){ $.getJSON("./source/student.json",function(stu){ //在“./source/student.json” $("#tab").empty(); $.each(stu,function(i,n){ $(‘#tab‘).append("<tr><td>"+n.name+"</td><td>"+n.age+"</td></tr>"); }); }); } </script> </head> <body> <button>点我刷新数据</button> <table> <tr> <th>姓名</th> <th>年龄</th> </tr> </table> <table style="border-style:solid;width:150px;height:20px;" id="tab"> <tr> <td></td> <td></td> </tr> </table> </body>
结果:
原文:http://www.cnblogs.com/shyroke/p/6480664.html