天天看點

jQuery的$.getScript方法去加載javaScript文檔解析

1.兩個檔案的代碼如下:

<script>
function Ajax(){    //将9-4.html中的Ajax()函數進行修改
  $.getScript('9-8.js',function(data){
  var html ="<table border='1' cellpadding='2'>";
  $.each(comments, function(Index, comment) {
      html += '<tr><td>' + comment.username + ':</td><td>' + comment['content'] + '</td></tr>';
  })        //comment['username']也可寫成comment.username
 html +="</table>"
//alert("Hello");
$("#target").html(html);
     }   
  );
}
</script>
<input type="button" value="Ajax送出" onclick="Ajax();" />
<div id="target"></div>      

2.

3. 解析:

comments 是個數組

comment  是個對象。

comments是數組,具體來說是json數組,而它的每個元素comment是json對象,并不是數組。既然是json對象,那麼取值的方法有2種:comment.attribute或者comment['attribute']

comments 是個數組
comment  是個對象。
我也是這樣認為的。

是不是對于json對象,引用其屬性有兩種方式,即comment.attribute或者comment['attribute']。

但是一般的對象,引用其屬性好像隻能是comment.attribute這種形式吧。

我不知道對于json對象還可以這樣引用comment['attribute']。我以為這樣就是數組了。

這就是json差別于數組的地方,自有它的特殊性。