1.兩個(gè)文件的代碼如下:
<script> function Ajax(){ //將9-4.html中的Ajax()函數(shù)進(jìn)行修改 $.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 是個(gè)數(shù)組
comment 是個(gè)對(duì)象。
comments是數(shù)組,具體來(lái)說(shuō)是json數(shù)組,而它的每個(gè)元素comment是json對(duì)象,并不是數(shù)組。既然是json對(duì)象,那么取值的方法有2種:comment.attribute或者comment['attribute']
comments 是個(gè)數(shù)組 comment 是個(gè)對(duì)象。 我也是這樣認(rèn)為的。 是不是對(duì)于json對(duì)象,引用其屬性有兩種方式,即comment.attribute或者comment['attribute']。 但是一般的對(duì)象,引用其屬性好像只能是comment.attribute這種形式吧。 我不知道對(duì)于json對(duì)象還可以這樣引用comment['attribute']。我以為這樣就是數(shù)組了。 這就是json區(qū)別于數(shù)組的地方,自有它的特殊性。
以上這篇基于jQuery的$.getScript方法去加載javaScript文檔解析就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號(hào)-2
違法及侵權(quán)請(qǐng)聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市萬(wàn)商天勤律師事務(wù)所王興未律師提供法律服務(wù)