当我在 firebug 中逐步执行此 javascript 代码 (scoreboard.js) 时,它可以很好地处理警报。当我没有在 firebug 中放置换行符并正常运行它时,我收到一条“favs is null”消息(没有警报)。
var favs = $.getJSON("favs.json");
favs = $.parseJSON(favs.responseText);
favs = favs.myTeams;
for (i=0; i<favs.length; i++){
alert(favs[i].text);
}
JSON (favs.json)
{"myTeams":[{"sport":10,"id":10,"abbrev":"NYY","isFav":false,"text":"New York Yankees","sw_abbrev":"nyy"},{"sport":28,"id":19,"abbrev":"nyg","isFav":false,"text":"New York Giants","sw_abbrev":"nyg"},{"sport":46,"id":18,"abbrev":"NY","isFav":false,"text":"New York Knicks","sw_abbrev":"nyk"},{"sport":90,"id":11,"abbrev":"NJ","isFav":false,"text":"New Jersey Devils","sw_abbrev":"njd"},{"sport":41,"id":2507,"abbrev":"PROV","isFav":false,"text":"Providence Friars"},{"sport":46,"id":17,"abbrev":"NJ","isFav":false,"text":"New Jersey Nets","sw_abbrev":"njn"},{"sport":600,"id":363,"abbrev":"Blues","isFav":false,"text":"Chelsea","sw_abbrev":"ENG.CHELSEA","isNational":false}]}
的HTML
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="scoreboard.js"></script>
</head>
<body>
Test
</body>
知道这里发生了什么吗?