<html?xmlns="http://www.w3.org/1999/xhtml">
????<head>
????????<title>使用getJSON()方法異步加載JSON格式數(shù)據(jù)</title>
????????<script?src="http://libs.baidu.com/jquery/1.9.0/jquery.js"?type="text/javascript"></script>
????????<link?href="style.css"?rel="stylesheet"?type="text/css"?/>
????</head>
????
????<body>
????????<div?id="divtest">
????????????<div?class="title">
????????????????<span?class="fl">我最喜歡的一項(xiàng)運(yùn)動(dòng)</span>?
????????????????<span?class="fr">
????????????????????<input?id="btnShow"?type="button"?value="加載"?/>
????????????????</span>
????????????</div>
????????????<ul></ul>
????????</div>
????????
????????<script?type="text/javascript">
????????????$(function?()?{
????????????????$("#btnShow").bind("click",?function?()?{
????????????????????var?$this?=?$(this);
????????????????????$.getJSON('sport.json',function(data){//回調(diào)函數(shù)
????????????????????????$this.attr("disabled",?"true");
????????????????????????$.each(data,?function?(index,?sport)?{
????????????????????????????$("ul").append("<li>"?+?sport["name"]?+?"</li>");
????????????????????????});
????
????????????????????});
????????????????})
????????????});
????????</script>
????</body>
</html>下面是sport.json文件里面的東西[{{"name" : "足球"},{"name" : "籃球"},{"name" : "乒乓球"}}]點(diǎn)擊加載之后,毫無反應(yīng),連$this.attr("disabled",?"true");都沒有執(zhí)行
1 回答
已采納

涂涂023
TA貢獻(xiàn)8條經(jīng)驗(yàn) 獲得超8個(gè)贊
修改下JSON和代碼 ,因?yàn)槟愕膉son代碼不是標(biāo)準(zhǔn)的json格式
json修改為
{ ????"result":[ ????????{"name"?:?"足球"}, ????????{"name"?:?"籃球"}, ????????{"name"?:?"乒乓球"} ?????] }
代碼在23行開始修改為
$.getJSON('sport.json',function(data){//回調(diào)函數(shù) ????????????????????????$this.attr("disabled",?"true"); ????????????????????????$.each(data.result,?function?(index,?sport)?{ ????????????????????????????$("ul").append("<li>"?+?sport["name"]?+?"</li>"); ????????????????????????}); ????? ????????????????????});
希望采納,有問題可 回復(fù)
- 1 回答
- 0 關(guān)注
- 1171 瀏覽
添加回答
舉報(bào)
0/150
提交
取消