关于jquery读取json文件
1 首先建一个json文件,[
{ "name":"张国立", "sex":"男", "email":"" }, { "name":"张铁林", "sex":"男", "email":"" }, { "name":"邓婕", "sex":"女", "email":"" } ]2 其次建一个getUserinfo.html页面用于获取JSON文件里的用户信息数据,并显示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>getJSON获取数据</title> <script type="text/javascript" src="https://code.jquery.com/jquery-3.0.0.min.js"></script> <style type="text/css"> #divframe { border: 1px solid #999; width: 500px; margin: 0 auto; } .loadTitle { background: #CCC; height: 30px; } </style> <script type="text/javascript"> $(function () { $("#btn").click(function () { $.getJSON("js/userinfo.json", function (data) { var $jsontip = $("#jsonTip"); var strHtml = "读取的数据:<br/>";//存储数据的变量 $jsontip.empty();//清空内容 $.each(data, function (haha, info) { strHtml += "姓名:" + info["name"] + "<br>"; strHtml += "性别:" + info["sex"] + "<br>"; strHtml += "邮箱:" + info["email"] + "<br>"; strHtml += "<hr>" }) $jsontip.html(strHtml);//显示处理后的数据 }) }) }) </script> </head> <body> <div id="divframe"> <div class="loadTitle"> <input type="button" value="获取数据" id="btn"/> </div> <div id="jsonTip"> </div> </div> </body> </html>3 运行效果如下 用浏览器打开getUserinfo.html文件,显示
相关推荐
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
84483065 2020-09-11
momode 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28
tthappyer 2020-07-25
TONIYH 2020-07-22
tztzyzyz 2020-07-20
83510998 2020-07-18
81463166 2020-07-17