這節(jié)中有兩個問題 第一:script中開頭的$(function()...有什么用,去掉一樣能運行。第二:代碼中$.each下面的if(index=='3'),這個3不加引號為什么不行,還有,if后面貌似沒有花括號還是能把下面的執(zhí)行語句包裹這是為什么?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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">我最喜歡的一項運動</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);
? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? $this.attr("disabled", "true");
? ? ? ? ? ? ? ? ? ? ? ? $.each(data, function (index, sport) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if(index==3)
? ? ? ? ? ? ? ? ? ? ? ? ? ? $("ul").append("<li>" + sport["name"] + "</li>");
? ? ? ? ? ? ? ? ? ? ? ? });
? ??
? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? })
? ? ? ? ? ? });
? ? ? ? </script>
? ? </body>
</html>
2017-03-14
語法好嘛,index==3是不用加引號的,if判斷里面如果語句塊只有一句 ,大括號是可以省略的。。。