风火家人开发记要

技术总结精华贴

Tag: 数组

html & html5, 原创

javascript(js)定义二维(多维)数组 在静态页面html(htm)中的数据设置与使用Javascript(js) Define Two-dimensional(Multi-dimensional) Array Set and Use On html(htm) Page

有的时候我们做静态页面需要设置数据,但又希望能够简单配置就可以,这个时候使用js数组来实现数据保存是最容易的事情了,下面简单举个例子来说明解决方法。 js 二维数组代码 var datalist = array(); datalist[0] = array(‘这里是数据1标题’,’这里是数据1的内容’,’数据1的作者’); datalist[1] = array(‘这里是数据2标题’,’这里是数据2的内容’,’数据2的作者’); 使用的时候可以直接使用 datalist[0][0]这样,也可以设置循环 使用js实现循环 var datanum = datalist.length; for(var i=0;i<datanum;i++){ document.write(‘ 标题:’+datalist[i][0]+’; 数据:’+datalist[i][1]+’;作者:’+datalist[i][2]+’ ‘); } 上面的数组是经过验证正确的使用方法 . 今天新增一个以对象方式添加的js 数组。看下面例子 var flashList = [ {“id”: 1, “showName”: “Publish Chat”, “flashName”: “videos/publish-chat.swf”, “pictureName”: “publish-chat-bg”}, {“id”: 2, “showName”: “General Features”, “flashName”: “videos/main-feature.swf”, “pictureName”: “features-bg”}, ]; for(var id in flashList) { alert(flashList[id].id); alert(flashList[id].showName); } 后面的这种方式 是我推荐使用的方式,没办法,工作需要。原文转至这里。 Sometimes we need set data on html page ,and hope it simple, use js is the simplest way. look this. js Two-dimensional array code var datalist = array(); datalist[0] = array(‘title 1′,’data 1′,’author 1’); datalist[1] = array(‘title 2′,’data 2′,’author 2’); you can use  datalist[0][0] ,and alse use a loop use js to loop var datanum = datalist.length; for(var i=0;i<datanum;i++){ document.write(‘ Title:’+datalist[i][0]+’; Data:’+datalist[i][1]+’; Author:’+datalist[i][2]+’ ‘); } it validate right to use. Another way to add js array as an Object. Look this. var flashList = [ {“id”: 1, “showName”: “Publish Chat”, “flashName”: “videos/publish-chat.swf”, “pictureName”: “publish-chat-bg”}, {“id”: 2, “showName”: “General Features”, “flashName”: “videos/main-feature.swf”, “pictureName”: “features-bg”}, ]; for(var id in flashList) { alert(flashList[id].id); alert(flashList[id].showName); } This way I suggest everyone to use. However Work needs. Text has been transferred to here. (655)

查看全文