<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> <script> function fn(data){ alert(data) }; </script> <script src="data.js"></script> </body> </html>
data.js 的内容是 fn([1,2,3,4,5])
输出结果:
实例:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script src="jquery1.8.3.min.js"></script> </head> <body> <dl id="Dl1"> </dl> <script> function fn(data) { var oDl1 = $(‘#Dl1‘); var html = ‘‘; $.each(data,function(index,content){ $.each(content,function(i,c){ html += ‘<dt>‘+c[‘title‘]+‘</dt>‘; html += ‘<dd>‘+c[‘summary‘]+‘</dd>‘; }) }) oDl1.html(html) }; var oScript = document.createElement(‘script‘); oScript.src = ‘https://api.douban.com/v2/book/search?q=javascript&count=10&callback=fn‘; document.body.appendChild(oScript); document.body.removeChild(oScript); </script> </body> </html>
处理数据:
//遍历数组 第一个参数是索引 第二个是内容
$.each( [0,1,2], function(i, n){
console.log( "索引:" + i + " 内容: " + n );
});
//遍历对象 第一个参数是key 第二个参数value
$.each( {name:"liujin","age":"24","sex":"boy"}, function(i, n){
console.log( "key:"+ i + " value: " + n );
});
时间: 2024-11-03 21:24:11