[关闭]
@GivenCui 2016-06-17T18:54:09.000000Z 字数 115506 阅读 1392

刘升旭经典课上demo

js上机练习



目录



JS课程合集跳转链接


名片管理器


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>名片管理器</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. /*
  10. 需求:名片管理器管理名片
  11. 构造函数:名片、名片管理器
  12. 名片属性:姓名、公司、电话、邮箱
  13. 名片管理器属性:名片数组
  14. 名片管理器方法:添加名片、显示所有名片信息、删除名片
  15. */
  16. //名片构造函数
  17. function Card (name, company, phoneNum, mail) {
  18. this.name = name;
  19. this.company = company;
  20. this.phoneNum = phoneNum;
  21. this.mail = mail;
  22. }
  23. //名片管理器构造函数
  24. function CardManager () {
  25. //名片数组的属性,用来存储名片的
  26. this.cards = new Array();
  27. }
  28. //添加名片的方法
  29. CardManager.prototype.addCard = function (newCard) {
  30. //把名片添加到管理器的名片数组里
  31. this.cards.push(newCard);
  32. }
  33. //显示所有名片信息的方法
  34. CardManager.prototype.showAllCardInfo = function () {
  35. //通过循环遍历卡片数组得到每个卡片对象
  36. for (var i = 0; i < this.cards.length; i++) {
  37. var tempCard = this.cards[i];
  38. //格式化输出卡片信息
  39. console.log(" \n 名字:" + tempCard.name + " \n 公司:" + tempCard.company + " \n 电话:" + tempCard.phoneNum + " \n 邮箱:" + tempCard.mail);
  40. }
  41. }
  42. //模仿indexOf方法
  43. Array.prototype.myIndexOf = function (removeCard) {
  44. //this指向的是当前的数组对象
  45. for (var i = 0; i < this.length; i++) {
  46. if (this[i] === removeCard) {
  47. return i;
  48. }
  49. }
  50. return -1;
  51. }
  52. //模拟其他面向对象语言的按照指定元素删除的方法
  53. Array.prototype.removeObj = function (removeObj) {
  54. //this指向的是当前的数组对象
  55. var idx = this.myIndexOf(removeObj);
  56. //判断找没找到
  57. if (idx > -1) {
  58. this.splice(idx, 1);
  59. return;
  60. }
  61. console.log("没有找到要删除的名片!");
  62. }
  63. //删除名片的方法(因为JS中,没有按照指定元素删除的方法,所以我们要通过给数组构造函数的原型添加额外的方法)
  64. CardManager.prototype.removeCard = function (removeCard) {
  65. //调用咱们自己写的删除方法
  66. this.cards.removeObj(removeCard);
  67. }
  68. //创建名片管理器对象
  69. var cardManager = new CardManager();
  70. //创建很多名片对象
  71. var card1 = new Card("刘轶佳", "育知同创", "18688889999", "liuyijia@163.com");
  72. var card2 = new Card("李慧玲", "育知同创", "13600005555", "lihuiling@126.com");
  73. var card3 = new Card("刘升旭", "苹果", "18612597091", "278305920@qq.com");
  74. //添加名片 (数组是有序可以重复的)
  75. cardManager.addCard(card2);
  76. cardManager.addCard(card3);
  77. cardManager.addCard(card1);
  78. //显示所有名片信息
  79. cardManager.showAllCardInfo();
  80. //删除指定名片
  81. cardManager.removeCard(card3);
  82. //打印分隔符
  83. console.log("---------------------");
  84. //显示所有名片信息
  85. cardManager.showAllCardInfo();
  86. var card4 = new Card("abc", "ddd", "1833", "sdf@174.com");
  87. //删除指定名片
  88. cardManager.removeCard(card4);
  89. //打印分隔符
  90. console.log("---------------------");
  91. //显示所有名片信息
  92. cardManager.showAllCardInfo();
  93. </script>
  94. </body>
  95. </html>

书签管理器


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>书签管理器</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. /*
  10. 需求:通过书签管理器来管理网页中的书签项。具备书签的增删改查功能。
  11. 构造函数:书签管理器、书签
  12. 书签的属性:网址、标题、星级
  13. 书签管理的属性:书签数组
  14. 书签管理的方法:
  15. 1、添加书签(要求:不能重复添加标题或网址,也就是说只要符合一个条件就不让添加)
  16. 2、显示所有标签信息(要求:按照星级排序)
  17. 3、删除标签(通过标题删除)
  18. 4、修改标签(通过标题去修改)
  19. */
  20. //书签构造函数
  21. function Book (url, title, star) {
  22. this.url = url;
  23. this.title = title;
  24. this.star = star;
  25. }
  26. //书签管理器构造函数
  27. function BookManager () {
  28. //属性:书签数组,用来存储很多书签的
  29. this.books = new Array();
  30. }
  31. //添加书签的方法(要求:不能重复添加标题或网址,也就是说只要符合一个条件就不让添加)
  32. BookManager.prototype.addBook = function (newBook) {
  33. //通过遍历得到书签数组中的每一个书签,目的是和新加的书签做判断。
  34. for (var i = 0; i < this.books.length; i++) {
  35. //得到每一个书签对象
  36. var tempBook = this.books[i];
  37. //判断新加的书签对象的标题或网址是否和之前存在的书签的标题或网址相同。
  38. if (tempBook.title === newBook.title || tempBook.url === newBook.url) {
  39. console.log("不能重复添加网址!");
  40. //return; 函数结束
  41. return;
  42. }
  43. }
  44. //经过循环判断没有重复的,就可以添加新书签
  45. this.books.push(newBook);
  46. }
  47. //显示所有标签信息的方法(要求:按照星级排序)
  48. BookManager.prototype.showAllBookInfo = function () {
  49. //冒泡排序,星级从小到大排序
  50. for (var i = 0; i < this.books.length - 1; i++) {
  51. for (var j = 0; j < this.books.length - 1 - i; j++) {
  52. //把位置为j 和 j +1的对象取出来
  53. var tempBook1 = this.books[j];
  54. var tempBook2 = this.books[j + 1];
  55. //判断相邻两个对象的星级属性大小
  56. if (tempBook1.star >= tempBook2.star) {
  57. //利用数组的splice函数,来实现数组两个位置元素的替换
  58. this.books.splice(j, 1, tempBook2);
  59. this.books.splice(j + 1, 1, tempBook1);
  60. }
  61. }
  62. }
  63. //格式化输出书签信息
  64. for (var i = 0; i < this.books.length; i++) {
  65. //得到每一个书签对象
  66. var tempBook = this.books[i];
  67. console.log(" \n 标题:" + tempBook.title + " \n 网址:" + tempBook.url + " \n 星级:" + tempBook.star);
  68. }
  69. }
  70. //给数组构造函数的原型添加方法(查找元素的下标)
  71. Array.prototype.myIndexOf = function (removeTitle) {
  72. for (var i = 0; i < this.length; i++) {
  73. //得当每一个数组里的对象
  74. var tempObj = this[i];
  75. //判断该对象的title属性 是否等于 要删除的title
  76. //如果相等,就说明找到要删除的对象了。返回其下标
  77. if (tempObj.title === removeTitle) {
  78. return i;
  79. }
  80. }
  81. return -1;
  82. }
  83. //给数组构造函数的原型添加方法(通过指定元素的标题删除)
  84. Array.prototype.removeObj = function (removeTitle) {
  85. var idx = this.myIndexOf(removeTitle);
  86. if (idx > -1) {
  87. this.splice(idx, 1);
  88. return;
  89. }
  90. console.log("没有找到该标题的书签!");
  91. }
  92. //删除书签(通过标题删除)
  93. BookManager.prototype.removeBook = function (removeTitle) {
  94. // for (var i = 0; i < this.books.length; i++) {
  95. // if (this.books[i].title === removeTitle) {
  96. // this.books.splice(i, 1);
  97. // break;
  98. // }
  99. // }
  100. this.books.removeObj(removeTitle);
  101. }
  102. //修改书签(通过标题去修改网址)
  103. BookManager.prototype.modifyBookUrlByTitle = function (targetTitle, modiyUrl) {
  104. //遍历书签数组,得到每一个书签对象
  105. for (var i = 0; i < this.books.length; i++) {
  106. //得到每一个书签对象
  107. var tempBook = this.books[i];
  108. //找到要修改的title书签对象
  109. if (tempBook.title === targetTitle) {
  110. //找到后,修改该书签的url
  111. tempBook.url = modiyUrl;
  112. return;
  113. }
  114. }
  115. console.log("没找到您要修改的书签!");
  116. }
  117. /************ 以上是构造函数相关 ************/
  118. //创建一个书签管理器对象
  119. var bookManager = new BookManager();
  120. //创建一堆书签对象
  121. var book1 = new Book("www.baidu.com", "百度", 4);
  122. var book2 = new Book("www.tencent.com", "腾讯", 5);
  123. var book3 = new Book("www.163.com", "网易", 2);
  124. var book4 = new Book("www.baidu.com", "必读", 3);
  125. var book5 = new Book("www.sina.com", "百度", 1);
  126. //添加书签
  127. bookManager.addBook(book1);
  128. //添加书签
  129. bookManager.addBook(book2);
  130. //添加书签
  131. bookManager.addBook(book3);
  132. //添加书签
  133. bookManager.addBook(book4);
  134. //添加书签
  135. bookManager.addBook(book5);
  136. //显示所有书签信息
  137. bookManager.showAllBookInfo();
  138. //删除书签
  139. bookManager.removeBook("腾讯");
  140. //打印分隔符
  141. console.log("===================");
  142. //显示所有书签信息
  143. bookManager.showAllBookInfo();
  144. //修改书签(通过标题修改网址)
  145. bookManager.modifyBookUrlByTitle("网易", "www.126.com");
  146. //打印分隔符
  147. console.log("===================");
  148. //显示所有书签信息
  149. bookManager.showAllBookInfo();
  150. //修改书签(通过标题修改网址)
  151. bookManager.modifyBookUrlByTitle("网易2", "www.120.com");
  152. //打印分隔符
  153. console.log("===================");
  154. //显示所有书签信息
  155. bookManager.showAllBookInfo();
  156. </script>
  157. </body>
  158. </html>

json文件


car.json

  1. {
  2. "ListContents" : [
  3. {
  4. "GroupCount" : "2",
  5. "GroupInfo" : [
  6. {
  7. "BrandID" : "1",
  8. "MainBrandName" : "奥迪",
  9. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/1.png"
  10. },
  11. {
  12. "BrandID" : "57",
  13. "MainBrandName" : "阿斯顿·马丁",
  14. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/57.png"
  15. }
  16. ],
  17. "PinYin" : "A"
  18. },
  19. {
  20. "GroupCount" : "16",
  21. "GroupInfo" : [
  22. {
  23. "BrandID" : "58",
  24. "MainBrandName" : "保时捷",
  25. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/58.png"
  26. },
  27. {
  28. "BrandID" : "59",
  29. "MainBrandName" : "宾利",
  30. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/59.png"
  31. },
  32. {
  33. "BrandID" : "2",
  34. "MainBrandName" : "北汽制造",
  35. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/2.png"
  36. },
  37. {
  38. "BrandID" : "3",
  39. "MainBrandName" : "奔驰",
  40. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/3.png"
  41. },
  42. {
  43. "BrandID" : "4",
  44. "MainBrandName" : "宝马",
  45. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/4.png"
  46. },
  47. {
  48. "BrandID" : "5",
  49. "MainBrandName" : "别克",
  50. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/5.png"
  51. },
  52. {
  53. "BrandID" : "6",
  54. "MainBrandName" : "比亚迪",
  55. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/6.png"
  56. },
  57. {
  58. "BrandID" : "21",
  59. "MainBrandName" : "本田",
  60. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/21.png"
  61. },
  62. {
  63. "BrandID" : "37",
  64. "MainBrandName" : "标致",
  65. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/37.png"
  66. },
  67. {
  68. "BrandID" : "107",
  69. "MainBrandName" : "奔腾",
  70. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/107.png"
  71. },
  72. {
  73. "BrandID" : "122",
  74. "MainBrandName" : "宝骏",
  75. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/122.png"
  76. },
  77. {
  78. "BrandID" : "138",
  79. "MainBrandName" : "巴博斯",
  80. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/138.png"
  81. },
  82. {
  83. "BrandID" : "135",
  84. "MainBrandName" : "北京汽车",
  85. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/135.png"
  86. },
  87. {
  88. "BrandID" : "150",
  89. "MainBrandName" : "北汽幻速",
  90. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/150.png"
  91. },
  92. {
  93. "BrandID" : "153",
  94. "MainBrandName" : "北汽威旺",
  95. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/153.png"
  96. },
  97. {
  98. "BrandID" : "156",
  99. "MainBrandName" : "北汽新能源",
  100. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/156.png"
  101. }
  102. ],
  103. "PinYin" : "B"
  104. },
  105. {
  106. "GroupCount" : "4",
  107. "GroupInfo" : [
  108. {
  109. "BrandID" : "116",
  110. "MainBrandName" : "长安汽车",
  111. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/116.png"
  112. },
  113. {
  114. "BrandID" : "7",
  115. "MainBrandName" : "长安商用",
  116. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/7.png"
  117. },
  118. {
  119. "BrandID" : "8",
  120. "MainBrandName" : "长城",
  121. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/8.png"
  122. },
  123. {
  124. "BrandID" : "10",
  125. "MainBrandName" : "昌河",
  126. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/10.png"
  127. }
  128. ],
  129. "PinYin" : "C"
  130. },
  131. {
  132. "GroupCount" : "10",
  133. "GroupInfo" : [
  134. {
  135. "BrandID" : "14",
  136. "MainBrandName" : "东风风行",
  137. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/14.png"
  138. },
  139. {
  140. "BrandID" : "44",
  141. "MainBrandName" : "东南",
  142. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/44.png"
  143. },
  144. {
  145. "BrandID" : "61",
  146. "MainBrandName" : "道奇",
  147. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/61.png"
  148. },
  149. {
  150. "BrandID" : "47",
  151. "MainBrandName" : "大众",
  152. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/47.png"
  153. },
  154. {
  155. "BrandID" : "93",
  156. "MainBrandName" : "大迪",
  157. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/93.png"
  158. },
  159. {
  160. "BrandID" : "117",
  161. "MainBrandName" : "东风小康",
  162. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/117.png"
  163. },
  164. {
  165. "BrandID" : "114",
  166. "MainBrandName" : "东风风神",
  167. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/114.png"
  168. },
  169. {
  170. "BrandID" : "146",
  171. "MainBrandName" : "东风风度",
  172. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/146.png"
  173. },
  174. {
  175. "BrandID" : "140",
  176. "MainBrandName" : "DS",
  177. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/140.png"
  178. },
  179. {
  180. "BrandID" : "141",
  181. "MainBrandName" : "东风御风",
  182. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/141.png"
  183. }
  184. ],
  185. "PinYin" : "D"
  186. },
  187. {
  188. "GroupCount" : "6",
  189. "GroupInfo" : [
  190. {
  191. "BrandID" : "62",
  192. "MainBrandName" : "法拉利",
  193. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/62.png"
  194. },
  195. {
  196. "BrandID" : "46",
  197. "MainBrandName" : "丰田",
  198. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/46.png"
  199. },
  200. {
  201. "BrandID" : "15",
  202. "MainBrandName" : "菲亚特",
  203. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/15.png"
  204. },
  205. {
  206. "BrandID" : "16",
  207. "MainBrandName" : "福特",
  208. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/16.png"
  209. },
  210. {
  211. "BrandID" : "17",
  212. "MainBrandName" : "福田",
  213. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/17.png"
  214. },
  215. {
  216. "BrandID" : "18",
  217. "MainBrandName" : "福迪",
  218. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/18.png"
  219. }
  220. ],
  221. "PinYin" : "F"
  222. },
  223. {
  224. "GroupCount" : "4",
  225. "GroupInfo" : [
  226. {
  227. "BrandID" : "55",
  228. "MainBrandName" : "广汽吉奥",
  229. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/55.png"
  230. },
  231. {
  232. "BrandID" : "115",
  233. "MainBrandName" : "GMC",
  234. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/115.png"
  235. },
  236. {
  237. "BrandID" : "120",
  238. "MainBrandName" : "广汽乘用车",
  239. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/120.png"
  240. },
  241. {
  242. "BrandID" : "133",
  243. "MainBrandName" : "观致",
  244. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/133.png"
  245. }
  246. ],
  247. "PinYin" : "G"
  248. },
  249. {
  250. "GroupCount" : "10",
  251. "GroupInfo" : [
  252. {
  253. "BrandID" : "119",
  254. "MainBrandName" : "海格",
  255. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/119.png"
  256. },
  257. {
  258. "BrandID" : "104",
  259. "MainBrandName" : "华泰汽车",
  260. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/104.png"
  261. },
  262. {
  263. "BrandID" : "143",
  264. "MainBrandName" : "哈弗",
  265. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/143.png"
  266. },
  267. {
  268. "BrandID" : "96",
  269. "MainBrandName" : "海马",
  270. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/96.png"
  271. },
  272. {
  273. "BrandID" : "64",
  274. "MainBrandName" : "悍马",
  275. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/64.png"
  276. },
  277. {
  278. "BrandID" : "22",
  279. "MainBrandName" : "红旗",
  280. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/22.png"
  281. },
  282. {
  283. "BrandID" : "43",
  284. "MainBrandName" : "华普",
  285. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/43.png"
  286. },
  287. {
  288. "BrandID" : "40",
  289. "MainBrandName" : "黄海汽车",
  290. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/40.png"
  291. },
  292. {
  293. "BrandID" : "41",
  294. "MainBrandName" : "汇众",
  295. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/41.png"
  296. },
  297. {
  298. "BrandID" : "20",
  299. "MainBrandName" : "哈飞",
  300. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/20.png"
  301. }
  302. ],
  303. "PinYin" : "H"
  304. },
  305. {
  306. "GroupCount" : "8",
  307. "GroupInfo" : [
  308. {
  309. "BrandID" : "25",
  310. "MainBrandName" : "江淮汽车",
  311. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/25.png"
  312. },
  313. {
  314. "BrandID" : "27",
  315. "MainBrandName" : "Jeep",
  316. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/27.png"
  317. },
  318. {
  319. "BrandID" : "29",
  320. "MainBrandName" : "金杯",
  321. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/29.png"
  322. },
  323. {
  324. "BrandID" : "30",
  325. "MainBrandName" : "江铃",
  326. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/30.png"
  327. },
  328. {
  329. "BrandID" : "19",
  330. "MainBrandName" : "吉利汽车",
  331. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/19.png"
  332. },
  333. {
  334. "BrandID" : "65",
  335. "MainBrandName" : "捷豹",
  336. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/65.png"
  337. },
  338. {
  339. "BrandID" : "144",
  340. "MainBrandName" : "金龙",
  341. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/144.png"
  342. },
  343. {
  344. "BrandID" : "127",
  345. "MainBrandName" : "九龙汽车",
  346. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/127.png"
  347. }
  348. ],
  349. "PinYin" : "J"
  350. },
  351. {
  352. "GroupCount" : "5",
  353. "GroupInfo" : [
  354. {
  355. "BrandID" : "108",
  356. "MainBrandName" : "开瑞",
  357. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/108.png"
  358. },
  359. {
  360. "BrandID" : "152",
  361. "MainBrandName" : "卡威",
  362. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/152.png"
  363. },
  364. {
  365. "BrandID" : "154",
  366. "MainBrandName" : "凯翼",
  367. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/154.png"
  368. },
  369. {
  370. "BrandID" : "66",
  371. "MainBrandName" : "凯迪拉克",
  372. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/66.png"
  373. },
  374. {
  375. "BrandID" : "67",
  376. "MainBrandName" : "克莱斯勒",
  377. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/67.png"
  378. }
  379. ],
  380. "PinYin" : "K"
  381. },
  382. {
  383. "GroupCount" : "12",
  384. "GroupInfo" : [
  385. {
  386. "BrandID" : "68",
  387. "MainBrandName" : "兰博基尼",
  388. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/68.png"
  389. },
  390. {
  391. "BrandID" : "70",
  392. "MainBrandName" : "劳斯莱斯",
  393. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/70.png"
  394. },
  395. {
  396. "BrandID" : "71",
  397. "MainBrandName" : "雷克萨斯",
  398. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/71.png"
  399. },
  400. {
  401. "BrandID" : "72",
  402. "MainBrandName" : "雷诺",
  403. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/72.png"
  404. },
  405. {
  406. "BrandID" : "74",
  407. "MainBrandName" : "林肯",
  408. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/74.png"
  409. },
  410. {
  411. "BrandID" : "75",
  412. "MainBrandName" : "路虎",
  413. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/75.png"
  414. },
  415. {
  416. "BrandID" : "90",
  417. "MainBrandName" : "力帆",
  418. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/90.png"
  419. },
  420. {
  421. "BrandID" : "31",
  422. "MainBrandName" : "陆风",
  423. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/31.png"
  424. },
  425. {
  426. "BrandID" : "45",
  427. "MainBrandName" : "铃木",
  428. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/45.png"
  429. },
  430. {
  431. "BrandID" : "121",
  432. "MainBrandName" : "理念",
  433. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/121.png"
  434. },
  435. {
  436. "BrandID" : "128",
  437. "MainBrandName" : "路特斯",
  438. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/128.png"
  439. },
  440. {
  441. "BrandID" : "126",
  442. "MainBrandName" : "猎豹汽车",
  443. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/126.png"
  444. }
  445. ],
  446. "PinYin" : "L"
  447. },
  448. {
  449. "GroupCount" : "5",
  450. "GroupInfo" : [
  451. {
  452. "BrandID" : "33",
  453. "MainBrandName" : "马自达",
  454. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/33.png"
  455. },
  456. {
  457. "BrandID" : "97",
  458. "MainBrandName" : "MG",
  459. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/97.png"
  460. },
  461. {
  462. "BrandID" : "77",
  463. "MainBrandName" : "玛莎拉蒂",
  464. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/77.png"
  465. },
  466. {
  467. "BrandID" : "78",
  468. "MainBrandName" : "迈巴赫",
  469. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/78.png"
  470. },
  471. {
  472. "BrandID" : "79",
  473. "MainBrandName" : "MINI",
  474. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/79.png"
  475. }
  476. ],
  477. "PinYin" : "M"
  478. },
  479. {
  480. "GroupCount" : "1",
  481. "GroupInfo" : [
  482. {
  483. "BrandID" : "118",
  484. "MainBrandName" : "纳智捷",
  485. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/118.png"
  486. }
  487. ],
  488. "PinYin" : "N"
  489. },
  490. {
  491. "GroupCount" : "3",
  492. "GroupInfo" : [
  493. {
  494. "BrandID" : "131",
  495. "MainBrandName" : "欧朗",
  496. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/131.png"
  497. },
  498. {
  499. "BrandID" : "80",
  500. "MainBrandName" : "欧宝",
  501. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/80.png"
  502. },
  503. {
  504. "BrandID" : "92",
  505. "MainBrandName" : "讴歌",
  506. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/92.png"
  507. }
  508. ],
  509. "PinYin" : "O"
  510. },
  511. {
  512. "GroupCount" : "4",
  513. "GroupInfo" : [
  514. {
  515. "BrandID" : "32",
  516. "MainBrandName" : "起亚",
  517. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/32.png"
  518. },
  519. {
  520. "BrandID" : "11",
  521. "MainBrandName" : "奇瑞",
  522. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/11.png"
  523. },
  524. {
  525. "BrandID" : "130",
  526. "MainBrandName" : "启辰",
  527. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/130.png"
  528. },
  529. {
  530. "BrandID" : "102",
  531. "MainBrandName" : "青年莲花",
  532. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/102.png"
  533. }
  534. ],
  535. "PinYin" : "Q"
  536. },
  537. {
  538. "GroupCount" : "3",
  539. "GroupInfo" : [
  540. {
  541. "BrandID" : "109",
  542. "MainBrandName" : "瑞麒",
  543. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/109.png"
  544. },
  545. {
  546. "BrandID" : "36",
  547. "MainBrandName" : "日产",
  548. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/36.png"
  549. },
  550. {
  551. "BrandID" : "94",
  552. "MainBrandName" : "荣威",
  553. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/94.png"
  554. }
  555. ],
  556. "PinYin" : "R"
  557. },
  558. {
  559. "GroupCount" : "12",
  560. "GroupInfo" : [
  561. {
  562. "BrandID" : "95",
  563. "MainBrandName" : "smart",
  564. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/95.png"
  565. },
  566. {
  567. "BrandID" : "98",
  568. "MainBrandName" : "世爵",
  569. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/98.png"
  570. },
  571. {
  572. "BrandID" : "82",
  573. "MainBrandName" : "萨博",
  574. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/82.png"
  575. },
  576. {
  577. "BrandID" : "83",
  578. "MainBrandName" : "双龙",
  579. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/83.png"
  580. },
  581. {
  582. "BrandID" : "84",
  583. "MainBrandName" : "斯巴鲁",
  584. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/84.png"
  585. },
  586. {
  587. "BrandID" : "85",
  588. "MainBrandName" : "斯柯达",
  589. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/85.png"
  590. },
  591. {
  592. "BrandID" : "34",
  593. "MainBrandName" : "三菱",
  594. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/34.png"
  595. },
  596. {
  597. "BrandID" : "42",
  598. "MainBrandName" : "双环",
  599. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/42.png"
  600. },
  601. {
  602. "BrandID" : "123",
  603. "MainBrandName" : "陕汽通家",
  604. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/123.png"
  605. },
  606. {
  607. "BrandID" : "145",
  608. "MainBrandName" : "上汽大通",
  609. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/145.png"
  610. },
  611. {
  612. "BrandID" : "136",
  613. "MainBrandName" : "思铭",
  614. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/136.png"
  615. },
  616. {
  617. "BrandID" : "148",
  618. "MainBrandName" : "绅宝",
  619. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/148.png"
  620. }
  621. ],
  622. "PinYin" : "S"
  623. },
  624. {
  625. "GroupCount" : "2",
  626. "GroupInfo" : [
  627. {
  628. "BrandID" : "149",
  629. "MainBrandName" : "特斯拉",
  630. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/149.png"
  631. },
  632. {
  633. "BrandID" : "147",
  634. "MainBrandName" : "腾势",
  635. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/147.png"
  636. }
  637. ],
  638. "PinYin" : "T"
  639. },
  640. {
  641. "GroupCount" : "6",
  642. "GroupInfo" : [
  643. {
  644. "BrandID" : "110",
  645. "MainBrandName" : "威麟",
  646. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/110.png"
  647. },
  648. {
  649. "BrandID" : "103",
  650. "MainBrandName" : "威兹曼",
  651. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/103.png"
  652. },
  653. {
  654. "BrandID" : "151",
  655. "MainBrandName" : "潍柴",
  656. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/151.png"
  657. },
  658. {
  659. "BrandID" : "86",
  660. "MainBrandName" : "沃尔沃",
  661. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/86.png"
  662. },
  663. {
  664. "BrandID" : "54",
  665. "MainBrandName" : "五十铃",
  666. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/54.png"
  667. },
  668. {
  669. "BrandID" : "49",
  670. "MainBrandName" : "五菱",
  671. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/49.png"
  672. }
  673. ],
  674. "PinYin" : "W"
  675. },
  676. {
  677. "GroupCount" : "7",
  678. "GroupInfo" : [
  679. {
  680. "BrandID" : "50",
  681. "MainBrandName" : "夏利",
  682. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/50.png"
  683. },
  684. {
  685. "BrandID" : "35",
  686. "MainBrandName" : "新雅途",
  687. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/35.png"
  688. },
  689. {
  690. "BrandID" : "12",
  691. "MainBrandName" : "雪佛兰",
  692. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/12.png"
  693. },
  694. {
  695. "BrandID" : "13",
  696. "MainBrandName" : "雪铁龙",
  697. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/13.png"
  698. },
  699. {
  700. "BrandID" : "23",
  701. "MainBrandName" : "现代",
  702. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/23.png"
  703. },
  704. {
  705. "BrandID" : "129",
  706. "MainBrandName" : "新凯",
  707. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/129.png"
  708. },
  709. {
  710. "BrandID" : "132",
  711. "MainBrandName" : "西雅特",
  712. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/132.png"
  713. }
  714. ],
  715. "PinYin" : "X"
  716. },
  717. {
  718. "GroupCount" : "6",
  719. "GroupInfo" : [
  720. {
  721. "BrandID" : "106",
  722. "MainBrandName" : "野马汽车",
  723. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/106.png"
  724. },
  725. {
  726. "BrandID" : "24",
  727. "MainBrandName" : "依维柯",
  728. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/24.png"
  729. },
  730. {
  731. "BrandID" : "53",
  732. "MainBrandName" : "一汽",
  733. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/53.png"
  734. },
  735. {
  736. "BrandID" : "99",
  737. "MainBrandName" : "永源汽车",
  738. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/99.png"
  739. },
  740. {
  741. "BrandID" : "101",
  742. "MainBrandName" : "扬子",
  743. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/101.png"
  744. },
  745. {
  746. "BrandID" : "87",
  747. "MainBrandName" : "英菲尼迪",
  748. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/87.png"
  749. }
  750. ],
  751. "PinYin" : "Y"
  752. },
  753. {
  754. "GroupCount" : "4",
  755. "GroupInfo" : [
  756. {
  757. "BrandID" : "91",
  758. "MainBrandName" : "众泰",
  759. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/91.png"
  760. },
  761. {
  762. "BrandID" : "51",
  763. "MainBrandName" : "中华",
  764. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/51.png"
  765. },
  766. {
  767. "BrandID" : "52",
  768. "MainBrandName" : "中兴",
  769. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/52.png"
  770. },
  771. {
  772. "BrandID" : "105",
  773. "MainBrandName" : "中誉",
  774. "imgURL" : "http://www.webcars.com.cn/CarsPhoto/Brands/0/105.png"
  775. }
  776. ],
  777. "PinYin" : "Z"
  778. }
  779. ],
  780. "ListCount" : "130"
  781. }

city.json

  1. {
  2. "城市代码" : [
  3. {
  4. "市" : [
  5. {
  6. "市名" : "北京",
  7. "编码" : "101010100"
  8. },
  9. {
  10. "市名" : "朝阳",
  11. "编码" : "101010300"
  12. },
  13. {
  14. "市名" : "顺义",
  15. "编码" : "101010400"
  16. },
  17. {
  18. "市名" : "怀柔",
  19. "编码" : "101010500"
  20. },
  21. {
  22. "市名" : "通州",
  23. "编码" : "101010600"
  24. },
  25. {
  26. "市名" : "昌平",
  27. "编码" : "101010700"
  28. },
  29. {
  30. "市名" : "延庆",
  31. "编码" : "101010800"
  32. },
  33. {
  34. "市名" : "丰台",
  35. "编码" : "101010900"
  36. },
  37. {
  38. "市名" : "石景山",
  39. "编码" : "101011000"
  40. },
  41. {
  42. "市名" : "大兴",
  43. "编码" : "101011100"
  44. },
  45. {
  46. "市名" : "房山",
  47. "编码" : "101011200"
  48. },
  49. {
  50. "市名" : "密云",
  51. "编码" : "101011300"
  52. },
  53. {
  54. "市名" : "门头沟",
  55. "编码" : "101011400"
  56. },
  57. {
  58. "市名" : "平谷",
  59. "编码" : "101011500"
  60. },
  61. {
  62. "市名" : "八达岭",
  63. "编码" : "101011600"
  64. },
  65. {
  66. "市名" : "佛爷顶",
  67. "编码" : "101011700"
  68. },
  69. {
  70. "市名" : "汤河口",
  71. "编码" : "101011800"
  72. },
  73. {
  74. "市名" : "密云上甸子",
  75. "编码" : "101011900"
  76. },
  77. {
  78. "市名" : "斋堂",
  79. "编码" : "101012000"
  80. },
  81. {
  82. "市名" : "霞云岭",
  83. "编码" : "101012100"
  84. },
  85. {
  86. "市名" : "北京城区",
  87. "编码" : "101012200"
  88. },
  89. {
  90. "市名" : "海淀",
  91. "编码" : "101010200"
  92. }
  93. ],
  94. "省" : "北京"
  95. },
  96. {
  97. "市" : [
  98. {
  99. "市名" : "天津",
  100. "编码" : "101030100"
  101. },
  102. {
  103. "市名" : "宝坻",
  104. "编码" : "101030300"
  105. },
  106. {
  107. "市名" : "东丽",
  108. "编码" : "101030400"
  109. },
  110. {
  111. "市名" : "西青",
  112. "编码" : "101030500"
  113. },
  114. {
  115. "市名" : "北辰",
  116. "编码" : "101030600"
  117. },
  118. {
  119. "市名" : "蓟县",
  120. "编码" : "101031400"
  121. },
  122. {
  123. "市名" : "汉沽",
  124. "编码" : "101030800"
  125. },
  126. {
  127. "市名" : "静海",
  128. "编码" : "101030900"
  129. },
  130. {
  131. "市名" : "津南",
  132. "编码" : "101031000"
  133. },
  134. {
  135. "市名" : "塘沽",
  136. "编码" : "101031100"
  137. },
  138. {
  139. "市名" : "大港",
  140. "编码" : "101031200"
  141. },
  142. {
  143. "市名" : "武清",
  144. "编码" : "101030200"
  145. },
  146. {
  147. "市名" : "宁河",
  148. "编码" : "101030700"
  149. }
  150. ],
  151. "省" : "天津市"
  152. },
  153. {
  154. "市" : [
  155. {
  156. "市名" : "上海",
  157. "编码" : "101020100"
  158. },
  159. {
  160. "市名" : "宝山",
  161. "编码" : "101020300"
  162. },
  163. {
  164. "市名" : "嘉定",
  165. "编码" : "101020500"
  166. },
  167. {
  168. "市名" : "南汇",
  169. "编码" : "101020600"
  170. },
  171. {
  172. "市名" : "浦东",
  173. "编码" : "101021300"
  174. },
  175. {
  176. "市名" : "青浦",
  177. "编码" : "101020800"
  178. },
  179. {
  180. "市名" : "松江",
  181. "编码" : "101020900"
  182. },
  183. {
  184. "市名" : "奉贤",
  185. "编码" : "101021000"
  186. },
  187. {
  188. "市名" : "崇明",
  189. "编码" : "101021100"
  190. },
  191. {
  192. "市名" : "徐家汇",
  193. "编码" : "101021200"
  194. },
  195. {
  196. "市名" : "闵行",
  197. "编码" : "101020200"
  198. },
  199. {
  200. "市名" : "金山",
  201. "编码" : "101020700"
  202. }
  203. ],
  204. "省" : "上海"
  205. },
  206. {
  207. "市" : [
  208. {
  209. "市名" : "石家庄",
  210. "编码" : "101090101"
  211. },
  212. {
  213. "市名" : "张家口",
  214. "编码" : "101090301"
  215. },
  216. {
  217. "市名" : "承德",
  218. "编码" : "101090402"
  219. },
  220. {
  221. "市名" : "唐山",
  222. "编码" : "101090501"
  223. },
  224. {
  225. "市名" : "秦皇岛",
  226. "编码" : "101091101"
  227. },
  228. {
  229. "市名" : "沧州",
  230. "编码" : "101090701"
  231. },
  232. {
  233. "市名" : "衡水",
  234. "编码" : "101090801"
  235. },
  236. {
  237. "市名" : "邢台",
  238. "编码" : "101090901"
  239. },
  240. {
  241. "市名" : "邯郸",
  242. "编码" : "101091001"
  243. },
  244. {
  245. "市名" : "保定",
  246. "编码" : "101090201"
  247. },
  248. {
  249. "市名" : "廊坊",
  250. "编码" : "101090601"
  251. }
  252. ],
  253. "省" : "河北"
  254. },
  255. {
  256. "市" : [
  257. {
  258. "市名" : "郑州",
  259. "编码" : "101180101"
  260. },
  261. {
  262. "市名" : "新乡",
  263. "编码" : "101180301"
  264. },
  265. {
  266. "市名" : "许昌",
  267. "编码" : "101180401"
  268. },
  269. {
  270. "市名" : "平顶山",
  271. "编码" : "101180501"
  272. },
  273. {
  274. "市名" : "信阳",
  275. "编码" : "101180601"
  276. },
  277. {
  278. "市名" : "南阳",
  279. "编码" : "101180701"
  280. },
  281. {
  282. "市名" : "开封",
  283. "编码" : "101180801"
  284. },
  285. {
  286. "市名" : "洛阳",
  287. "编码" : "101180901"
  288. },
  289. {
  290. "市名" : "商丘",
  291. "编码" : "101181001"
  292. },
  293. {
  294. "市名" : "焦作",
  295. "编码" : "101181101"
  296. },
  297. {
  298. "市名" : "鹤壁",
  299. "编码" : "101181201"
  300. },
  301. {
  302. "市名" : "濮阳",
  303. "编码" : "101181301"
  304. },
  305. {
  306. "市名" : "周口",
  307. "编码" : "101181401"
  308. },
  309. {
  310. "市名" : "漯河",
  311. "编码" : "101181501"
  312. },
  313. {
  314. "市名" : "驻马店",
  315. "编码" : "101181601"
  316. },
  317. {
  318. "市名" : "三门峡",
  319. "编码" : "101181701"
  320. },
  321. {
  322. "市名" : "济源",
  323. "编码" : "101181801"
  324. },
  325. {
  326. "市名" : "安阳",
  327. "编码" : "101180201"
  328. }
  329. ],
  330. "省" : "河南"
  331. },
  332. {
  333. "市" : [
  334. {
  335. "市名" : "合肥",
  336. "编码" : "101220101"
  337. },
  338. {
  339. "市名" : "芜湖",
  340. "编码" : "101220301"
  341. },
  342. {
  343. "市名" : "淮南",
  344. "编码" : "101220401"
  345. },
  346. {
  347. "市名" : "马鞍山",
  348. "编码" : "101220501"
  349. },
  350. {
  351. "市名" : "安庆",
  352. "编码" : "101220601"
  353. },
  354. {
  355. "市名" : "宿州",
  356. "编码" : "101220701"
  357. },
  358. {
  359. "市名" : "阜阳",
  360. "编码" : "101220801"
  361. },
  362. {
  363. "市名" : "亳州",
  364. "编码" : "101220901"
  365. },
  366. {
  367. "市名" : "黄山",
  368. "编码" : "101221001"
  369. },
  370. {
  371. "市名" : "滁州",
  372. "编码" : "101221101"
  373. },
  374. {
  375. "市名" : "淮北",
  376. "编码" : "101221201"
  377. },
  378. {
  379. "市名" : "铜陵",
  380. "编码" : "101221301"
  381. },
  382. {
  383. "市名" : "宣城",
  384. "编码" : "101221401"
  385. },
  386. {
  387. "市名" : "六安",
  388. "编码" : "101221501"
  389. },
  390. {
  391. "市名" : "巢湖",
  392. "编码" : "101221601"
  393. },
  394. {
  395. "市名" : "池州",
  396. "编码" : "101221701"
  397. },
  398. {
  399. "市名" : "蚌埠",
  400. "编码" : "101220201"
  401. }
  402. ],
  403. "省" : "安徽"
  404. },
  405. {
  406. "市" : [
  407. {
  408. "市名" : "杭州",
  409. "编码" : "101210101"
  410. },
  411. {
  412. "市名" : "舟山",
  413. "编码" : "101211101"
  414. },
  415. {
  416. "市名" : "湖州",
  417. "编码" : "101210201"
  418. },
  419. {
  420. "市名" : "嘉兴",
  421. "编码" : "101210301"
  422. },
  423. {
  424. "市名" : "金华",
  425. "编码" : "101210901"
  426. },
  427. {
  428. "市名" : "绍兴",
  429. "编码" : "101210501"
  430. },
  431. {
  432. "市名" : "台州",
  433. "编码" : "101210601"
  434. },
  435. {
  436. "市名" : "温州",
  437. "编码" : "101210701"
  438. },
  439. {
  440. "市名" : "丽水",
  441. "编码" : "101210801"
  442. },
  443. {
  444. "市名" : "衢州",
  445. "编码" : "101211001"
  446. },
  447. {
  448. "市名" : "宁波",
  449. "编码" : "101210401"
  450. }
  451. ],
  452. "省" : "浙江"
  453. },
  454. {
  455. "市" : [
  456. {
  457. "市名" : "重庆",
  458. "编码" : "101040100"
  459. },
  460. {
  461. "市名" : "合川",
  462. "编码" : "101040300"
  463. },
  464. {
  465. "市名" : "南川",
  466. "编码" : "101040400"
  467. },
  468. {
  469. "市名" : "江津",
  470. "编码" : "101040500"
  471. },
  472. {
  473. "市名" : "万盛",
  474. "编码" : "101040600"
  475. },
  476. {
  477. "市名" : "渝北",
  478. "编码" : "101040700"
  479. },
  480. {
  481. "市名" : "北碚",
  482. "编码" : "101040800"
  483. },
  484. {
  485. "市名" : "巴南",
  486. "编码" : "101040900"
  487. },
  488. {
  489. "市名" : "长寿",
  490. "编码" : "101041000"
  491. },
  492. {
  493. "市名" : "黔江",
  494. "编码" : "101041100"
  495. },
  496. {
  497. "市名" : "万州天城",
  498. "编码" : "101041200"
  499. },
  500. {
  501. "市名" : "万州龙宝",
  502. "编码" : "101041300"
  503. },
  504. {
  505. "市名" : "涪陵",
  506. "编码" : "101041400"
  507. },
  508. {
  509. "市名" : "开县",
  510. "编码" : "101041500"
  511. },
  512. {
  513. "市名" : "城口",
  514. "编码" : "101041600"
  515. },
  516. {
  517. "市名" : "云阳",
  518. "编码" : "101041700"
  519. },
  520. {
  521. "市名" : "巫溪",
  522. "编码" : "101041800"
  523. },
  524. {
  525. "市名" : "奉节",
  526. "编码" : "101041900"
  527. },
  528. {
  529. "市名" : "巫山",
  530. "编码" : "101042000"
  531. },
  532. {
  533. "市名" : "潼南",
  534. "编码" : "101042100"
  535. },
  536. {
  537. "市名" : "垫江",
  538. "编码" : "101042200"
  539. },
  540. {
  541. "市名" : "梁平",
  542. "编码" : "101042300"
  543. },
  544. {
  545. "市名" : "忠县",
  546. "编码" : "101042400"
  547. },
  548. {
  549. "市名" : "石柱",
  550. "编码" : "101042500"
  551. },
  552. {
  553. "市名" : "大足",
  554. "编码" : "101042600"
  555. },
  556. {
  557. "市名" : "荣昌",
  558. "编码" : "101042700"
  559. },
  560. {
  561. "市名" : "铜梁",
  562. "编码" : "101042800"
  563. },
  564. {
  565. "市名" : "璧山",
  566. "编码" : "101042900"
  567. },
  568. {
  569. "市名" : "丰都",
  570. "编码" : "101043000"
  571. },
  572. {
  573. "市名" : "武隆",
  574. "编码" : "101043100"
  575. },
  576. {
  577. "市名" : "彭水",
  578. "编码" : "101043200"
  579. },
  580. {
  581. "市名" : "綦江",
  582. "编码" : "101043300"
  583. },
  584. {
  585. "市名" : "酉阳",
  586. "编码" : "101043400"
  587. },
  588. {
  589. "市名" : "秀山",
  590. "编码" : "101043600"
  591. },
  592. {
  593. "市名" : "沙坪坝",
  594. "编码" : "101043700"
  595. },
  596. {
  597. "市名" : "永川",
  598. "编码" : "101040200"
  599. }
  600. ],
  601. "省" : "重庆"
  602. },
  603. {
  604. "市" : [
  605. {
  606. "市名" : "福州",
  607. "编码" : "101230101"
  608. },
  609. {
  610. "市名" : "泉州",
  611. "编码" : "101230501"
  612. },
  613. {
  614. "市名" : "漳州",
  615. "编码" : "101230601"
  616. },
  617. {
  618. "市名" : "龙岩",
  619. "编码" : "101230701"
  620. },
  621. {
  622. "市名" : "晋江",
  623. "编码" : "101230509"
  624. },
  625. {
  626. "市名" : "南平",
  627. "编码" : "101230901"
  628. },
  629. {
  630. "市名" : "厦门",
  631. "编码" : "101230201"
  632. },
  633. {
  634. "市名" : "宁德",
  635. "编码" : "101230301"
  636. },
  637. {
  638. "市名" : "莆田",
  639. "编码" : "101230401"
  640. },
  641. {
  642. "市名" : "三明",
  643. "编码" : "101230801"
  644. }
  645. ],
  646. "省" : "福建"
  647. },
  648. {
  649. "市" : [
  650. {
  651. "市名" : "兰州",
  652. "编码" : "101160101"
  653. },
  654. {
  655. "市名" : "平凉",
  656. "编码" : "101160301"
  657. },
  658. {
  659. "市名" : "庆阳",
  660. "编码" : "101160401"
  661. },
  662. {
  663. "市名" : "武威",
  664. "编码" : "101160501"
  665. },
  666. {
  667. "市名" : "金昌",
  668. "编码" : "101160601"
  669. },
  670. {
  671. "市名" : "嘉峪关",
  672. "编码" : "101161401"
  673. },
  674. {
  675. "市名" : "酒泉",
  676. "编码" : "101160801"
  677. },
  678. {
  679. "市名" : "天水",
  680. "编码" : "101160901"
  681. },
  682. {
  683. "市名" : "武都",
  684. "编码" : "101161001"
  685. },
  686. {
  687. "市名" : "临夏",
  688. "编码" : "101161101"
  689. },
  690. {
  691. "市名" : "合作",
  692. "编码" : "101161201"
  693. },
  694. {
  695. "市名" : "白银",
  696. "编码" : "101161301"
  697. },
  698. {
  699. "市名" : "定西",
  700. "编码" : "101160201"
  701. },
  702. {
  703. "市名" : "张掖",
  704. "编码" : "101160701"
  705. }
  706. ],
  707. "省" : "甘肃"
  708. },
  709. {
  710. "市" : [
  711. {
  712. "市名" : "广州",
  713. "编码" : "101280101"
  714. },
  715. {
  716. "市名" : "惠州",
  717. "编码" : "101280301"
  718. },
  719. {
  720. "市名" : "梅州",
  721. "编码" : "101280401"
  722. },
  723. {
  724. "市名" : "汕头",
  725. "编码" : "101280501"
  726. },
  727. {
  728. "市名" : "深圳",
  729. "编码" : "101280601"
  730. },
  731. {
  732. "市名" : "珠海",
  733. "编码" : "101280701"
  734. },
  735. {
  736. "市名" : "佛山",
  737. "编码" : "101280800"
  738. },
  739. {
  740. "市名" : "肇庆",
  741. "编码" : "101280901"
  742. },
  743. {
  744. "市名" : "湛江",
  745. "编码" : "101281001"
  746. },
  747. {
  748. "市名" : "江门",
  749. "编码" : "101281101"
  750. },
  751. {
  752. "市名" : "河源",
  753. "编码" : "101281201"
  754. },
  755. {
  756. "市名" : "清远",
  757. "编码" : "101281301"
  758. },
  759. {
  760. "市名" : "云浮",
  761. "编码" : "101281401"
  762. },
  763. {
  764. "市名" : "潮州",
  765. "编码" : "101281501"
  766. },
  767. {
  768. "市名" : "东莞",
  769. "编码" : "101281601"
  770. },
  771. {
  772. "市名" : "中山",
  773. "编码" : "101281701"
  774. },
  775. {
  776. "市名" : "阳江",
  777. "编码" : "101281801"
  778. },
  779. {
  780. "市名" : "揭阳",
  781. "编码" : "101281901"
  782. },
  783. {
  784. "市名" : "茂名",
  785. "编码" : "101282001"
  786. },
  787. {
  788. "市名" : "汕尾",
  789. "编码" : "101282101"
  790. },
  791. {
  792. "市名" : "韶关",
  793. "编码" : "101280201"
  794. }
  795. ],
  796. "省" : "广东"
  797. },
  798. {
  799. "市" : [
  800. {
  801. "市名" : "南宁",
  802. "编码" : "101300101"
  803. },
  804. {
  805. "市名" : "柳州",
  806. "编码" : "101300301"
  807. },
  808. {
  809. "市名" : "来宾",
  810. "编码" : "101300401"
  811. },
  812. {
  813. "市名" : "桂林",
  814. "编码" : "101300501"
  815. },
  816. {
  817. "市名" : "梧州",
  818. "编码" : "101300601"
  819. },
  820. {
  821. "市名" : "防城港",
  822. "编码" : "101301401"
  823. },
  824. {
  825. "市名" : "贵港",
  826. "编码" : "101300801"
  827. },
  828. {
  829. "市名" : "玉林",
  830. "编码" : "101300901"
  831. },
  832. {
  833. "市名" : "百色",
  834. "编码" : "101301001"
  835. },
  836. {
  837. "市名" : "钦州",
  838. "编码" : "101301101"
  839. },
  840. {
  841. "市名" : "河池",
  842. "编码" : "101301201"
  843. },
  844. {
  845. "市名" : "北海",
  846. "编码" : "101301301"
  847. },
  848. {
  849. "市名" : "崇左",
  850. "编码" : "101300201"
  851. },
  852. {
  853. "市名" : "贺州",
  854. "编码" : "101300701"
  855. }
  856. ],
  857. "省" : "广西"
  858. },
  859. {
  860. "市" : [
  861. {
  862. "市名" : "贵阳",
  863. "编码" : "101260101"
  864. },
  865. {
  866. "市名" : "安顺",
  867. "编码" : "101260301"
  868. },
  869. {
  870. "市名" : "都匀",
  871. "编码" : "101260401"
  872. },
  873. {
  874. "市名" : "兴义",
  875. "编码" : "101260906"
  876. },
  877. {
  878. "市名" : "铜仁",
  879. "编码" : "101260601"
  880. },
  881. {
  882. "市名" : "毕节",
  883. "编码" : "101260701"
  884. },
  885. {
  886. "市名" : "六盘水",
  887. "编码" : "101260801"
  888. },
  889. {
  890. "市名" : "遵义",
  891. "编码" : "101260201"
  892. },
  893. {
  894. "市名" : "凯里",
  895. "编码" : "101260501"
  896. }
  897. ],
  898. "省" : "贵州"
  899. },
  900. {
  901. "市" : [
  902. {
  903. "市名" : "昆明",
  904. "编码" : "101290101"
  905. },
  906. {
  907. "市名" : "红河",
  908. "编码" : "101290301"
  909. },
  910. {
  911. "市名" : "文山",
  912. "编码" : "101290601"
  913. },
  914. {
  915. "市名" : "玉溪",
  916. "编码" : "101290701"
  917. },
  918. {
  919. "市名" : "楚雄",
  920. "编码" : "101290801"
  921. },
  922. {
  923. "市名" : "普洱",
  924. "编码" : "101290901"
  925. },
  926. {
  927. "市名" : "昭通",
  928. "编码" : "101291001"
  929. },
  930. {
  931. "市名" : "临沧",
  932. "编码" : "101291101"
  933. },
  934. {
  935. "市名" : "怒江",
  936. "编码" : "101291201"
  937. },
  938. {
  939. "市名" : "香格里拉",
  940. "编码" : "101291301"
  941. },
  942. {
  943. "市名" : "丽江",
  944. "编码" : "101291401"
  945. },
  946. {
  947. "市名" : "德宏",
  948. "编码" : "101291501"
  949. },
  950. {
  951. "市名" : "景洪",
  952. "编码" : "101291601"
  953. },
  954. {
  955. "市名" : "大理",
  956. "编码" : "101290201"
  957. },
  958. {
  959. "市名" : "曲靖",
  960. "编码" : "101290401"
  961. },
  962. {
  963. "市名" : "保山",
  964. "编码" : "101290501"
  965. }
  966. ],
  967. "省" : "云南"
  968. },
  969. {
  970. "市" : [
  971. {
  972. "市名" : "呼和浩特",
  973. "编码" : "101080101"
  974. },
  975. {
  976. "市名" : "乌海",
  977. "编码" : "101080301"
  978. },
  979. {
  980. "市名" : "集宁",
  981. "编码" : "101080401"
  982. },
  983. {
  984. "市名" : "通辽",
  985. "编码" : "101080501"
  986. },
  987. {
  988. "市名" : "阿拉善左旗",
  989. "编码" : "101081201"
  990. },
  991. {
  992. "市名" : "鄂尔多斯",
  993. "编码" : "101080701"
  994. },
  995. {
  996. "市名" : "临河",
  997. "编码" : "101080801"
  998. },
  999. {
  1000. "市名" : "锡林浩特",
  1001. "编码" : "101080901"
  1002. },
  1003. {
  1004. "市名" : "呼伦贝尔",
  1005. "编码" : "101081000"
  1006. },
  1007. {
  1008. "市名" : "乌兰浩特",
  1009. "编码" : "101081101"
  1010. },
  1011. {
  1012. "市名" : "包头",
  1013. "编码" : "101080201"
  1014. },
  1015. {
  1016. "市名" : "赤峰",
  1017. "编码" : "101080601"
  1018. }
  1019. ],
  1020. "省" : "内蒙古"
  1021. },
  1022. {
  1023. "市" : [
  1024. {
  1025. "市名" : "南昌",
  1026. "编码" : "101240101"
  1027. },
  1028. {
  1029. "市名" : "上饶",
  1030. "编码" : "101240301"
  1031. },
  1032. {
  1033. "市名" : "抚州",
  1034. "编码" : "101240401"
  1035. },
  1036. {
  1037. "市名" : "宜春",
  1038. "编码" : "101240501"
  1039. },
  1040. {
  1041. "市名" : "鹰潭",
  1042. "编码" : "101241101"
  1043. },
  1044. {
  1045. "市名" : "赣州",
  1046. "编码" : "101240701"
  1047. },
  1048. {
  1049. "市名" : "景德镇",
  1050. "编码" : "101240801"
  1051. },
  1052. {
  1053. "市名" : "萍乡",
  1054. "编码" : "101240901"
  1055. },
  1056. {
  1057. "市名" : "新余",
  1058. "编码" : "101241001"
  1059. },
  1060. {
  1061. "市名" : "九江",
  1062. "编码" : "101240201"
  1063. },
  1064. {
  1065. "市名" : "吉安",
  1066. "编码" : "101240601"
  1067. }
  1068. ],
  1069. "省" : "江西"
  1070. },
  1071. {
  1072. "市" : [
  1073. {
  1074. "市名" : "武汉",
  1075. "编码" : "101200101"
  1076. },
  1077. {
  1078. "市名" : "黄冈",
  1079. "编码" : "101200501"
  1080. },
  1081. {
  1082. "市名" : "荆州",
  1083. "编码" : "101200801"
  1084. },
  1085. {
  1086. "市名" : "宜昌",
  1087. "编码" : "101200901"
  1088. },
  1089. {
  1090. "市名" : "恩施",
  1091. "编码" : "101201001"
  1092. },
  1093. {
  1094. "市名" : "十堰",
  1095. "编码" : "101201101"
  1096. },
  1097. {
  1098. "市名" : "神农架",
  1099. "编码" : "101201201"
  1100. },
  1101. {
  1102. "市名" : "随州",
  1103. "编码" : "101201301"
  1104. },
  1105. {
  1106. "市名" : "荆门",
  1107. "编码" : "101201401"
  1108. },
  1109. {
  1110. "市名" : "天门",
  1111. "编码" : "101201501"
  1112. },
  1113. {
  1114. "市名" : "仙桃",
  1115. "编码" : "101201601"
  1116. },
  1117. {
  1118. "市名" : "潜江",
  1119. "编码" : "101201701"
  1120. },
  1121. {
  1122. "市名" : "襄樊",
  1123. "编码" : "101200201"
  1124. },
  1125. {
  1126. "市名" : "鄂州",
  1127. "编码" : "101200301"
  1128. },
  1129. {
  1130. "市名" : "孝感",
  1131. "编码" : "101200401"
  1132. },
  1133. {
  1134. "市名" : "黄石",
  1135. "编码" : "101200601"
  1136. },
  1137. {
  1138. "市名" : "咸宁",
  1139. "编码" : "101200701"
  1140. }
  1141. ],
  1142. "省" : "湖北"
  1143. },
  1144. {
  1145. "市" : [
  1146. {
  1147. "市名" : "成都",
  1148. "编码" : "101270101"
  1149. },
  1150. {
  1151. "市名" : "自贡",
  1152. "编码" : "101270301"
  1153. },
  1154. {
  1155. "市名" : "绵阳",
  1156. "编码" : "101270401"
  1157. },
  1158. {
  1159. "市名" : "南充",
  1160. "编码" : "101270501"
  1161. },
  1162. {
  1163. "市名" : "达州",
  1164. "编码" : "101270601"
  1165. },
  1166. {
  1167. "市名" : "遂宁",
  1168. "编码" : "101270701"
  1169. },
  1170. {
  1171. "市名" : "广安",
  1172. "编码" : "101270801"
  1173. },
  1174. {
  1175. "市名" : "巴中",
  1176. "编码" : "101270901"
  1177. },
  1178. {
  1179. "市名" : "泸州",
  1180. "编码" : "101271001"
  1181. },
  1182. {
  1183. "市名" : "宜宾",
  1184. "编码" : "101271101"
  1185. },
  1186. {
  1187. "市名" : "内江",
  1188. "编码" : "101271201"
  1189. },
  1190. {
  1191. "市名" : "资阳",
  1192. "编码" : "101271301"
  1193. },
  1194. {
  1195. "市名" : "乐山",
  1196. "编码" : "101271401"
  1197. },
  1198. {
  1199. "市名" : "眉山",
  1200. "编码" : "101271501"
  1201. },
  1202. {
  1203. "市名" : "凉山",
  1204. "编码" : "101271601"
  1205. },
  1206. {
  1207. "市名" : "雅安",
  1208. "编码" : "101271701"
  1209. },
  1210. {
  1211. "市名" : "甘孜",
  1212. "编码" : "101271801"
  1213. },
  1214. {
  1215. "市名" : "阿坝",
  1216. "编码" : "101271901"
  1217. },
  1218. {
  1219. "市名" : "德阳",
  1220. "编码" : "101272001"
  1221. },
  1222. {
  1223. "市名" : "广元",
  1224. "编码" : "101272101"
  1225. },
  1226. {
  1227. "市名" : "攀枝花",
  1228. "编码" : "101270201"
  1229. }
  1230. ],
  1231. "省" : "四川"
  1232. },
  1233. {
  1234. "市" : [
  1235. {
  1236. "市名" : "银川",
  1237. "编码" : "101170101"
  1238. },
  1239. {
  1240. "市名" : "中卫",
  1241. "编码" : "101170501"
  1242. },
  1243. {
  1244. "市名" : "固原",
  1245. "编码" : "101170401"
  1246. },
  1247. {
  1248. "市名" : "石嘴山",
  1249. "编码" : "101170201"
  1250. },
  1251. {
  1252. "市名" : "吴忠",
  1253. "编码" : "101170301"
  1254. }
  1255. ],
  1256. "省" : "宁夏"
  1257. },
  1258. {
  1259. "市" : [
  1260. {
  1261. "市名" : "西宁",
  1262. "编码" : "101150101"
  1263. },
  1264. {
  1265. "市名" : "黄南",
  1266. "编码" : "101150301"
  1267. },
  1268. {
  1269. "市名" : "海北",
  1270. "编码" : "101150801"
  1271. },
  1272. {
  1273. "市名" : "果洛",
  1274. "编码" : "101150501"
  1275. },
  1276. {
  1277. "市名" : "玉树",
  1278. "编码" : "101150601"
  1279. },
  1280. {
  1281. "市名" : "海西",
  1282. "编码" : "101150701"
  1283. },
  1284. {
  1285. "市名" : "海东",
  1286. "编码" : "101150201"
  1287. },
  1288. {
  1289. "市名" : "海南",
  1290. "编码" : "101150401"
  1291. }
  1292. ],
  1293. "省" : "青海省"
  1294. },
  1295. {
  1296. "市" : [
  1297. {
  1298. "市名" : "济南",
  1299. "编码" : "101120101"
  1300. },
  1301. {
  1302. "市名" : "潍坊",
  1303. "编码" : "101120601"
  1304. },
  1305. {
  1306. "市名" : "临沂",
  1307. "编码" : "101120901"
  1308. },
  1309. {
  1310. "市名" : "菏泽",
  1311. "编码" : "101121001"
  1312. },
  1313. {
  1314. "市名" : "滨州",
  1315. "编码" : "101121101"
  1316. },
  1317. {
  1318. "市名" : "东营",
  1319. "编码" : "101121201"
  1320. },
  1321. {
  1322. "市名" : "威海",
  1323. "编码" : "101121301"
  1324. },
  1325. {
  1326. "市名" : "枣庄",
  1327. "编码" : "101121401"
  1328. },
  1329. {
  1330. "市名" : "日照",
  1331. "编码" : "101121501"
  1332. },
  1333. {
  1334. "市名" : "莱芜",
  1335. "编码" : "101121601"
  1336. },
  1337. {
  1338. "市名" : "聊城",
  1339. "编码" : "101121701"
  1340. },
  1341. {
  1342. "市名" : "青岛",
  1343. "编码" : "101120201"
  1344. },
  1345. {
  1346. "市名" : "淄博",
  1347. "编码" : "101120301"
  1348. },
  1349. {
  1350. "市名" : "德州",
  1351. "编码" : "101120401"
  1352. },
  1353. {
  1354. "市名" : "烟台",
  1355. "编码" : "101120501"
  1356. },
  1357. {
  1358. "市名" : "济宁",
  1359. "编码" : "101120701"
  1360. },
  1361. {
  1362. "市名" : "泰安",
  1363. "编码" : "101120801"
  1364. }
  1365. ],
  1366. "省" : "山东"
  1367. },
  1368. {
  1369. "市" : [
  1370. {
  1371. "市名" : "西安",
  1372. "编码" : "101110101"
  1373. },
  1374. {
  1375. "市名" : "延安",
  1376. "编码" : "101110300"
  1377. },
  1378. {
  1379. "市名" : "榆林",
  1380. "编码" : "101110401"
  1381. },
  1382. {
  1383. "市名" : "铜川",
  1384. "编码" : "101111001"
  1385. },
  1386. {
  1387. "市名" : "商洛",
  1388. "编码" : "101110601"
  1389. },
  1390. {
  1391. "市名" : "安康",
  1392. "编码" : "101110701"
  1393. },
  1394. {
  1395. "市名" : "汉中",
  1396. "编码" : "101110801"
  1397. },
  1398. {
  1399. "市名" : "宝鸡",
  1400. "编码" : "101110901"
  1401. },
  1402. {
  1403. "市名" : "咸阳",
  1404. "编码" : "101110200"
  1405. },
  1406. {
  1407. "市名" : "渭南",
  1408. "编码" : "101110501"
  1409. }
  1410. ],
  1411. "省" : "陕西省"
  1412. },
  1413. {
  1414. "市" : [
  1415. {
  1416. "市名" : "太原",
  1417. "编码" : "101100101"
  1418. },
  1419. {
  1420. "市名" : "临汾",
  1421. "编码" : "101100701"
  1422. },
  1423. {
  1424. "市名" : "运城",
  1425. "编码" : "101100801"
  1426. },
  1427. {
  1428. "市名" : "朔州",
  1429. "编码" : "101100901"
  1430. },
  1431. {
  1432. "市名" : "忻州",
  1433. "编码" : "101101001"
  1434. },
  1435. {
  1436. "市名" : "长治",
  1437. "编码" : "101100501"
  1438. },
  1439. {
  1440. "市名" : "大同",
  1441. "编码" : "101100201"
  1442. },
  1443. {
  1444. "市名" : "阳泉",
  1445. "编码" : "101100301"
  1446. },
  1447. {
  1448. "市名" : "晋中",
  1449. "编码" : "101100401"
  1450. },
  1451. {
  1452. "市名" : "晋城",
  1453. "编码" : "101100601"
  1454. },
  1455. {
  1456. "市名" : "吕梁",
  1457. "编码" : "101101100"
  1458. }
  1459. ],
  1460. "省" : "山西"
  1461. },
  1462. {
  1463. "市" : [
  1464. {
  1465. "市名" : "乌鲁木齐",
  1466. "编码" : "101130101"
  1467. },
  1468. {
  1469. "市名" : "石河子",
  1470. "编码" : "101130301"
  1471. },
  1472. {
  1473. "市名" : "昌吉",
  1474. "编码" : "101130401"
  1475. },
  1476. {
  1477. "市名" : "吐鲁番",
  1478. "编码" : "101130501"
  1479. },
  1480. {
  1481. "市名" : "库尔勒",
  1482. "编码" : "101130601"
  1483. },
  1484. {
  1485. "市名" : "阿拉尔",
  1486. "编码" : "101130701"
  1487. },
  1488. {
  1489. "市名" : "阿克苏",
  1490. "编码" : "101130801"
  1491. },
  1492. {
  1493. "市名" : "喀什",
  1494. "编码" : "101130901"
  1495. },
  1496. {
  1497. "市名" : "伊宁",
  1498. "编码" : "101131001"
  1499. },
  1500. {
  1501. "市名" : "塔城",
  1502. "编码" : "101131101"
  1503. },
  1504. {
  1505. "市名" : "哈密",
  1506. "编码" : "101131201"
  1507. },
  1508. {
  1509. "市名" : "和田",
  1510. "编码" : "101131301"
  1511. },
  1512. {
  1513. "市名" : "阿勒泰",
  1514. "编码" : "101131401"
  1515. },
  1516. {
  1517. "市名" : "阿图什",
  1518. "编码" : "101131501"
  1519. },
  1520. {
  1521. "市名" : "博乐",
  1522. "编码" : "101131601"
  1523. },
  1524. {
  1525. "市名" : "克拉玛依",
  1526. "编码" : "101130201"
  1527. }
  1528. ],
  1529. "省" : "新疆"
  1530. },
  1531. {
  1532. "市" : [
  1533. {
  1534. "市名" : "拉萨",
  1535. "编码" : "101140101"
  1536. },
  1537. {
  1538. "市名" : "山南",
  1539. "编码" : "101140301"
  1540. },
  1541. {
  1542. "市名" : "阿里",
  1543. "编码" : "101140701"
  1544. },
  1545. {
  1546. "市名" : "昌都",
  1547. "编码" : "101140501"
  1548. },
  1549. {
  1550. "市名" : "那曲",
  1551. "编码" : "101140601"
  1552. },
  1553. {
  1554. "市名" : "日喀则",
  1555. "编码" : "101140201"
  1556. },
  1557. {
  1558. "市名" : "林芝",
  1559. "编码" : "101140401"
  1560. }
  1561. ],
  1562. "省" : "西藏"
  1563. },
  1564. {
  1565. "市" : [
  1566. {
  1567. "市名" : "台北县",
  1568. "编码" : "101340101"
  1569. },
  1570. {
  1571. "市名" : "高雄",
  1572. "编码" : "101340201"
  1573. },
  1574. {
  1575. "市名" : "台中",
  1576. "编码" : "101340401"
  1577. }
  1578. ],
  1579. "省" : "台湾"
  1580. },
  1581. {
  1582. "市" : [
  1583. {
  1584. "市名" : "海口",
  1585. "编码" : "101310101"
  1586. },
  1587. {
  1588. "市名" : "三亚",
  1589. "编码" : "101310201"
  1590. },
  1591. {
  1592. "市名" : "东方",
  1593. "编码" : "101310202"
  1594. },
  1595. {
  1596. "市名" : "临高",
  1597. "编码" : "101310203"
  1598. },
  1599. {
  1600. "市名" : "澄迈",
  1601. "编码" : "101310204"
  1602. },
  1603. {
  1604. "市名" : "儋州",
  1605. "编码" : "101310205"
  1606. },
  1607. {
  1608. "市名" : "昌江",
  1609. "编码" : "101310206"
  1610. },
  1611. {
  1612. "市名" : "白沙",
  1613. "编码" : "101310207"
  1614. },
  1615. {
  1616. "市名" : "琼中",
  1617. "编码" : "101310208"
  1618. },
  1619. {
  1620. "市名" : "定安",
  1621. "编码" : "101310209"
  1622. },
  1623. {
  1624. "市名" : "屯昌",
  1625. "编码" : "101310210"
  1626. },
  1627. {
  1628. "市名" : "琼海",
  1629. "编码" : "101310211"
  1630. },
  1631. {
  1632. "市名" : "文昌",
  1633. "编码" : "101310212"
  1634. },
  1635. {
  1636. "市名" : "保亭",
  1637. "编码" : "101310214"
  1638. },
  1639. {
  1640. "市名" : "万宁",
  1641. "编码" : "101310215"
  1642. },
  1643. {
  1644. "市名" : "陵水",
  1645. "编码" : "101310216"
  1646. },
  1647. {
  1648. "市名" : "西沙",
  1649. "编码" : "101310217"
  1650. },
  1651. {
  1652. "市名" : "南沙岛",
  1653. "编码" : "101310220"
  1654. },
  1655. {
  1656. "市名" : "乐东",
  1657. "编码" : "101310221"
  1658. },
  1659. {
  1660. "市名" : "五指山",
  1661. "编码" : "101310222"
  1662. },
  1663. {
  1664. "市名" : "琼山",
  1665. "编码" : "101310102"
  1666. }
  1667. ],
  1668. "省" : "海南省"
  1669. },
  1670. {
  1671. "市" : [
  1672. {
  1673. "市名" : "长沙",
  1674. "编码" : "101250101"
  1675. },
  1676. {
  1677. "市名" : "株洲",
  1678. "编码" : "101250301"
  1679. },
  1680. {
  1681. "市名" : "衡阳",
  1682. "编码" : "101250401"
  1683. },
  1684. {
  1685. "市名" : "郴州",
  1686. "编码" : "101250501"
  1687. },
  1688. {
  1689. "市名" : "常德",
  1690. "编码" : "101250601"
  1691. },
  1692. {
  1693. "市名" : "益阳",
  1694. "编码" : "101250700"
  1695. },
  1696. {
  1697. "市名" : "娄底",
  1698. "编码" : "101250801"
  1699. },
  1700. {
  1701. "市名" : "邵阳",
  1702. "编码" : "101250901"
  1703. },
  1704. {
  1705. "市名" : "岳阳",
  1706. "编码" : "101251001"
  1707. },
  1708. {
  1709. "市名" : "张家界",
  1710. "编码" : "101251101"
  1711. },
  1712. {
  1713. "市名" : "怀化",
  1714. "编码" : "101251201"
  1715. },
  1716. {
  1717. "市名" : "黔阳",
  1718. "编码" : "101251301"
  1719. },
  1720. {
  1721. "市名" : "永州",
  1722. "编码" : "101251401"
  1723. },
  1724. {
  1725. "市名" : "吉首",
  1726. "编码" : "101251501"
  1727. },
  1728. {
  1729. "市名" : "湘潭",
  1730. "编码" : "101250201"
  1731. }
  1732. ],
  1733. "省" : "湖南"
  1734. },
  1735. {
  1736. "市" : [
  1737. {
  1738. "市名" : "南京",
  1739. "编码" : "101190101"
  1740. },
  1741. {
  1742. "市名" : "镇江",
  1743. "编码" : "101190301"
  1744. },
  1745. {
  1746. "市名" : "苏州",
  1747. "编码" : "101190401"
  1748. },
  1749. {
  1750. "市名" : "南通",
  1751. "编码" : "101190501"
  1752. },
  1753. {
  1754. "市名" : "扬州",
  1755. "编码" : "101190601"
  1756. },
  1757. {
  1758. "市名" : "宿迁",
  1759. "编码" : "101191301"
  1760. },
  1761. {
  1762. "市名" : "徐州",
  1763. "编码" : "101190801"
  1764. },
  1765. {
  1766. "市名" : "淮安",
  1767. "编码" : "101190901"
  1768. },
  1769. {
  1770. "市名" : "连云港",
  1771. "编码" : "101191001"
  1772. },
  1773. {
  1774. "市名" : "常州",
  1775. "编码" : "101191101"
  1776. },
  1777. {
  1778. "市名" : "泰州",
  1779. "编码" : "101191201"
  1780. },
  1781. {
  1782. "市名" : "无锡",
  1783. "编码" : "101190201"
  1784. },
  1785. {
  1786. "市名" : "盐城",
  1787. "编码" : "101190701"
  1788. }
  1789. ],
  1790. "省" : "江苏"
  1791. },
  1792. {
  1793. "市" : [
  1794. {
  1795. "市名" : "哈尔滨",
  1796. "编码" : "101050101"
  1797. },
  1798. {
  1799. "市名" : "牡丹江",
  1800. "编码" : "101050301"
  1801. },
  1802. {
  1803. "市名" : "佳木斯",
  1804. "编码" : "101050401"
  1805. },
  1806. {
  1807. "市名" : "绥化",
  1808. "编码" : "101050501"
  1809. },
  1810. {
  1811. "市名" : "黑河",
  1812. "编码" : "101050601"
  1813. },
  1814. {
  1815. "市名" : "双鸭山",
  1816. "编码" : "101051301"
  1817. },
  1818. {
  1819. "市名" : "伊春",
  1820. "编码" : "101050801"
  1821. },
  1822. {
  1823. "市名" : "大庆",
  1824. "编码" : "101050901"
  1825. },
  1826. {
  1827. "市名" : "七台河",
  1828. "编码" : "101051002"
  1829. },
  1830. {
  1831. "市名" : "鸡西",
  1832. "编码" : "101051101"
  1833. },
  1834. {
  1835. "市名" : "鹤岗",
  1836. "编码" : "101051201"
  1837. },
  1838. {
  1839. "市名" : "齐齐哈尔",
  1840. "编码" : "101050201"
  1841. },
  1842. {
  1843. "市名" : "大兴安岭",
  1844. "编码" : "101050701"
  1845. }
  1846. ],
  1847. "省" : "黑龙江"
  1848. },
  1849. {
  1850. "市" : [
  1851. {
  1852. "市名" : "长春",
  1853. "编码" : "101060101"
  1854. },
  1855. {
  1856. "市名" : "延吉",
  1857. "编码" : "101060301"
  1858. },
  1859. {
  1860. "市名" : "四平",
  1861. "编码" : "101060401"
  1862. },
  1863. {
  1864. "市名" : "白山",
  1865. "编码" : "101060901"
  1866. },
  1867. {
  1868. "市名" : "白城",
  1869. "编码" : "101060601"
  1870. },
  1871. {
  1872. "市名" : "辽源",
  1873. "编码" : "101060701"
  1874. },
  1875. {
  1876. "市名" : "松原",
  1877. "编码" : "101060801"
  1878. },
  1879. {
  1880. "市名" : "吉林",
  1881. "编码" : "101060201"
  1882. },
  1883. {
  1884. "市名" : "通化",
  1885. "编码" : "101060501"
  1886. }
  1887. ],
  1888. "省" : "吉林"
  1889. },
  1890. {
  1891. "市" : [
  1892. {
  1893. "市名" : "沈阳",
  1894. "编码" : "101070101"
  1895. },
  1896. {
  1897. "市名" : "鞍山",
  1898. "编码" : "101070301"
  1899. },
  1900. {
  1901. "市名" : "抚顺",
  1902. "编码" : "101070401"
  1903. },
  1904. {
  1905. "市名" : "本溪",
  1906. "编码" : "101070501"
  1907. },
  1908. {
  1909. "市名" : "丹东",
  1910. "编码" : "101070601"
  1911. },
  1912. {
  1913. "市名" : "葫芦岛",
  1914. "编码" : "101071401"
  1915. },
  1916. {
  1917. "市名" : "营口",
  1918. "编码" : "101070801"
  1919. },
  1920. {
  1921. "市名" : "阜新",
  1922. "编码" : "101070901"
  1923. },
  1924. {
  1925. "市名" : "辽阳",
  1926. "编码" : "101071001"
  1927. },
  1928. {
  1929. "市名" : "铁岭",
  1930. "编码" : "101071101"
  1931. },
  1932. {
  1933. "市名" : "朝阳",
  1934. "编码" : "101071201"
  1935. },
  1936. {
  1937. "市名" : "盘锦",
  1938. "编码" : "101071301"
  1939. },
  1940. {
  1941. "市名" : "大连",
  1942. "编码" : "101070201"
  1943. },
  1944. {
  1945. "市名" : "锦州",
  1946. "编码" : "101070701"
  1947. }
  1948. ],
  1949. "省" : "辽宁"
  1950. }
  1951. ]
  1952. }

解析城市json


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>作业</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //根据city.json 手动写一个JS字面量对象,并且封装到数据模型中
  10. var rootObj = {
  11. //省份数组
  12. provinces : [
  13. //每个省份对象
  14. {
  15. provinceName : "黑龙江省",
  16. //城市数组
  17. cities : [
  18. //每个城市对象
  19. {
  20. cityName : "哈尔滨市",
  21. cityCode : "1001"
  22. },
  23. {
  24. cityName : "大庆市",
  25. cityCode : "1002"
  26. },
  27. {
  28. cityName : "鸡西市",
  29. cityCode : "1003"
  30. }
  31. ]
  32. },
  33. {
  34. provinceName : "河北省",
  35. cities : [
  36. {
  37. cityName : "石家庄",
  38. cityCode : "2001"
  39. },
  40. {
  41. cityName : "廊坊市",
  42. cityCode : "2002"
  43. }
  44. ]
  45. }
  46. ]
  47. }
  48. //省份构造函数
  49. function Province (provinceName) {
  50. this.provinceName = provinceName;
  51. //城市数组
  52. this.cities = new Array();
  53. }
  54. //城市构造函数
  55. function City (cityName, cityCode) {
  56. this.cityName = cityName;
  57. this.cityCode = cityCode;
  58. }
  59. //存储省份对象的数组
  60. var provinceArray = new Array();
  61. //解析字面量对象、并且封装数据模型
  62. //获得省份数组
  63. var provinces = rootObj.provinces;
  64. //遍历省份数组
  65. for (var tempProvinceIdx in provinces) {
  66. //得到每一个省份对象
  67. var tempProvince = provinces[tempProvinceIdx];
  68. /** 创建省份对象(构造函数) **/
  69. var tempProvinceObj = new Province(tempProvince.provinceName);
  70. /** 把省份对象添加到省份数组中 **/
  71. provinceArray.push(tempProvinceObj);
  72. //获得城市数组
  73. var cities = tempProvince.cities;
  74. //遍历城市数组
  75. for (var tempCityIdx in cities) {
  76. //得到每一个城市对象
  77. var tempCity = cities[tempCityIdx];
  78. /** 创建城市对象(构造函数) **/
  79. var tempCityObj = new City(tempCity.cityName, tempCity.cityCode);
  80. /** 把每个城市对象添加到当前省的城市数组中 **/
  81. tempProvinceObj.cities.push(tempCityObj);
  82. // 遍历城市对象
  83. for (var tempCityProperty in tempCity) {
  84. //找某个城市
  85. if (tempCityProperty === 'cityName') {
  86. //获得城市名字
  87. var cityName = tempCity[tempCityProperty];
  88. if (cityName === '石家庄') {
  89. //格式化输出
  90. console.log(" \n 省份:" + tempProvince.provinceName + " \n 城市:" + tempCity.cityName + " \n 编码:" + tempCity.cityCode);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. console.log(provinceArray);
  97. </script>
  98. </body>
  99. </html>

AjaxUtil


  1. /*
  2. * 封装ajax
  3. * method: 请求方式 GET/POST
  4. * url: 请求路径
  5. * data: 参数
  6. * successCallBackFn: 响应成功回调函数
  7. * failCallBackFn: 响应失败回调函数
  8. */
  9. function ajaxFn (method, url, data, successCallBackFn, failCallBackFn) {
  10. var xhr;
  11. //创建请求对象
  12. if (window.XMLHttpRequest) {
  13. xhr = new XMLHttpRequest();
  14. } else if (window.ActiveXObject) {
  15. var versions = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
  16. for (var i = 0; i < versions.length; i++) {
  17. try {
  18. xhr = new ActiveXObject(versions[i]);
  19. break;
  20. } catch (e) {
  21. console.log(e);
  22. }
  23. }
  24. } else {
  25. //创建一个错误对象
  26. //throw 是主动抛出一个异常错误
  27. throw new Error("浏览器不支持AJAX");
  28. }
  29. //打开链接 和 发送请求
  30. if (method == "GET" || method == "get") {
  31. //利用ajax GET请求会有缓存,为了避免每次访问的路径不一样。我们可以在
  32. //url后面加上 Math.random()来解决。
  33. xhr.open(method, url + "?" + data + Math.random(), true);
  34. xhr.send(null);
  35. } else if (method == "POST" || method == "post") {
  36. xhr.open(method, url, true);
  37. //post请求需要设置请求头
  38. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  39. xhr.send(data);
  40. } else {
  41. console.log("请求方式不对!");
  42. }
  43. //请求响应结果
  44. xhr.onreadystatechange = function () {
  45. if (xhr.readyState == 4 ) {
  46. if (xhr.status == 200) {
  47. //回调成功的函数
  48. successCallBackFn(xhr.responseText);
  49. } else {
  50. //失败
  51. failCallBackFn("失败!");
  52. }
  53. }
  54. }
  55. return xhr;
  56. }

EventUtil


  1. /*
  2. * 事件封装工具
  3. */
  4. var EventUtil = {
  5. //添加事件的方法
  6. /*
  7. * element: 给"谁"(某个元素)添加事件
  8. * eventType: 事件类型
  9. * handler: 具体实现函数
  10. */
  11. addHandler : function (element, eventType, handler) {
  12. //三种实现
  13. if (window.addEventListener) {
  14. //IE9及IE9以上
  15. element.addEventListener(eventType, handler);
  16. } else if (window.attachEvent) {
  17. //IE8及IE8以下实现
  18. //attachEvent需要的事件类型 需要 加上 on
  19. element.attachEvent("on" + eventType, handler);
  20. } else {
  21. //通过中括号访问属性的语法才可以
  22. element["on" + eventType] = handler;
  23. }
  24. },
  25. removeHandler : function (element, eventType, handler) {
  26. if (window.removeEventListener) {
  27. element.removeEventListener(eventType, handler);
  28. } else if (window.detachEvent) {
  29. element.detachEvent("on" + eventType, handler);
  30. } else {
  31. element["on" + eventType] = null;
  32. }
  33. }
  34. }

解析carjson


  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>解析carJson</title>
  6. <script type="text/javascript" src="js/AjaxUtil.js" ></script>
  7. <script type="text/javascript" src="js/EventUtil.js" ></script>
  8. </head>
  9. <body>
  10. <script type="text/javascript">
  11. //通过工具类,给window添加onload事件
  12. EventUtil.addHandler(window, "load", function () {
  13. // alert($("load_btn").value);
  14. //执行完onload事件后,给button添加单击事件
  15. EventUtil.addHandler($("load_btn"), "click", function () {
  16. //点击按钮通过ajax来异步加载数据
  17. // ajaxFn("GET", "car.json", null, function () {
  18. // 可以这么写 这个是成功
  19. // }, function () {
  20. // 这个是失败
  21. // });
  22. //调用ajax异步请求数据
  23. ajaxFn("GET", "car.json", null, successCallBackFn,
  24. failCallBackFn);
  25. });
  26. });
  27. //ajax返回的成功函数
  28. function successCallBackFn (returnValue) {
  29. // console.log(returnValue);
  30. //把JSON字符串转换成JS对象
  31. var rootObj = JSON.parse(returnValue);
  32. // console.log(rootObj);
  33. //得到listContents数组
  34. var listContentArray = rootObj.ListContents;
  35. //获得外层table
  36. var outTable = $("out_table");
  37. //遍历listContentArray
  38. for (var tempListContentIdx in listContentArray) {
  39. //得到每一个listContent对象
  40. var tempListContent = listContentArray[tempListContentIdx];
  41. //创建外层tr
  42. var outTr = createElement("tr");
  43. //创建外层td
  44. var outTd = createElement("td");
  45. //创建内层table
  46. var inTable = createElement("table");
  47. //创建内层thead
  48. var inThead = createElement("thead");
  49. //创建内层thead的textNode
  50. var inTheadValue = createTextNode(tempListContent.PinYin);
  51. //添加textNode
  52. inThead.appendChild(inTheadValue);
  53. //添加inThead
  54. inTable.appendChild(inThead);
  55. //添加inTable
  56. outTd.appendChild(inTable);
  57. //添加outTd
  58. outTr.appendChild(outTd);
  59. //添加outTr
  60. outTable.appendChild(outTr);
  61. /******** 创建内部的tr 和 td *******/
  62. //得到carArray数组
  63. var carArray = tempListContent.GroupInfo;
  64. //遍历carArray
  65. for (var tempCarIdx in carArray) {
  66. //得到每一个car对象
  67. var tempCar = carArray[tempCarIdx];
  68. //创建内部tr
  69. var inTr = createElement("tr");
  70. //创建内部td
  71. var inTd = createElement("td");
  72. //创建内部td的textNode
  73. var inTdValue = createTextNode(tempCar.MainBrandName);
  74. //添加textNode
  75. inTd.appendChild(inTdValue);
  76. //添加inTd
  77. inTr.appendChild(inTd);
  78. //添加inTr
  79. inTable.appendChild(inTr);
  80. }
  81. }
  82. }
  83. //ajax返回的失败函数
  84. function failCallBackFn (returnValue) {
  85. alert(returnValue);
  86. }
  87. //封装一下通过id获取标签元素
  88. function $ (idValue) {
  89. return document.getElementById(idValue);
  90. }
  91. //封装一下创建元素的方法
  92. function createElement (elementName) {
  93. return document.createElement(elementName);
  94. }
  95. //封装一下创建textNode
  96. function createTextNode (elementValue) {
  97. return document.createTextNode(elementValue);
  98. }
  99. </script>
  100. <input type="button" value="加载数据" id="load_btn"/>
  101. <br />
  102. <br />
  103. <table id="out_table" border="1px" >
  104. <!--<tr>
  105. <td>
  106. <table>
  107. <thead>A</thead>
  108. <tr>
  109. <td>阿拉斯加</td>
  110. </tr>
  111. <tr>
  112. <td>阿拉丁</td>
  113. </tr>
  114. </table>
  115. </td>
  116. </tr>
  117. <tr>
  118. <td>
  119. <table>
  120. <thead>B</thead>
  121. <tr>
  122. <td>宝马</td>
  123. </tr>
  124. <tr>
  125. <td>奔驰</td>
  126. </tr>
  127. </table>
  128. </td>
  129. </tr>-->
  130. </table>
  131. </body>
  132. </html>

左右选人


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>选人</title>
  6. <style type="text/css">
  7. div {
  8. float: left;
  9. border: 1px solid red;
  10. }
  11. #main_div {
  12. width: 500px;
  13. height: 300px;
  14. margin-left: 300px;
  15. }
  16. #left_div {
  17. width: 200px;
  18. height: 300px;
  19. }
  20. #center_div {
  21. width: 94px;
  22. height: 300px;
  23. text-align: center;
  24. }
  25. #right_div {
  26. width: 200px;
  27. height: 300px;
  28. float: right;
  29. }
  30. #left_sel {
  31. width: 200px;
  32. height: 280px;
  33. }
  34. #right_sel {
  35. width: 200px;
  36. height: 280px;
  37. }
  38. input {
  39. margin-top: 30px;
  40. }
  41. </style>
  42. <!-- 导入事件工具js文件 -->
  43. <script type="text/javascript" src="EventUtil.js"></script>
  44. </head>
  45. <body>
  46. <div id="main_div">
  47. <!-- 左边的div -->
  48. <div id="left_div">
  49. <label id="left_label">0</label>
  50. <select id="left_sel" multiple>
  51. </select>
  52. </div>
  53. <!-- 中间的div -->
  54. <div id="center_div">
  55. <input type="button" value="添加" id="add_btn" />
  56. <br />
  57. <input type="button" value="移除" id="remove_btn" />
  58. <br />
  59. <input type="button" value="全部添加" id="addAll_btn" />
  60. <br />
  61. <input type="button" value="全部移除" id="removeAll_btn" />
  62. </div>
  63. <!-- 右边的div -->
  64. <div id="right_div">
  65. <label id="right_label">0</label>
  66. <select id="right_sel" multiple>
  67. </select>
  68. </div>
  69. </div>
  70. <script type="text/javascript">
  71. //左边sel
  72. var leftSel = $("left_sel");
  73. //左边数据数组
  74. var leftPersonArray = new Array();
  75. //右边sel
  76. var rightSel = $("right_sel");
  77. //右边数据数组
  78. var rightPersonArray = new Array();
  79. //window的onload事件
  80. EventUtil.addHandler(window, "load", function () {
  81. //1、初始化数据
  82. var personObj = [
  83. {
  84. name : "张三"
  85. },
  86. {
  87. name : "李四"
  88. },
  89. {
  90. name : "王五"
  91. },
  92. {
  93. name : "赵六"
  94. },
  95. {
  96. name : "冯七"
  97. }
  98. ];
  99. //遍历personObj
  100. for (var tempIdx in personObj) {
  101. //得到每个person对象
  102. var tempPerson = personObj[tempIdx];
  103. //添加到左边数据数组
  104. leftPersonArray.push(tempPerson);
  105. //重置左边sel
  106. resetSel(leftSel, tempPerson.name);
  107. //人数
  108. leftAndRightCount();
  109. }
  110. //2、给左边sel添加事件
  111. EventUtil.addHandler(leftSel, "dblclick", function () {
  112. //添加右边数据数组
  113. // this.selectedIndex 相当于leftSel.selectedIndex,获得选中option的下标
  114. rightPersonArray.push(leftPersonArray[this.selectedIndex]);
  115. //重置右边sel
  116. resetSel(rightSel, leftPersonArray[this.selectedIndex].name);
  117. //删除左边数据数组
  118. leftPersonArray.splice(this.selectedIndex, 1);
  119. //删除左边option
  120. //this.options 获得所有option 是一个数组
  121. leftSel.removeChild(this.options[this.selectedIndex]);
  122. //人数
  123. leftAndRightCount();
  124. });
  125. //3、给添加按钮添加事件
  126. EventUtil.addHandler($("add_btn"), "click", function () {
  127. //遍历options
  128. for (var i = 0; i < leftSel.options.length; i++) {
  129. //找出选中的option
  130. if (leftSel.options[i].selected) {
  131. //添加右边数据数组
  132. rightPersonArray.push(leftPersonArray[i]);
  133. //重置右边sel的方法
  134. resetSel(rightSel, leftPersonArray[i].name);
  135. //删除左边数据数组
  136. leftPersonArray.splice(i, 1);
  137. //删除左边的option
  138. leftSel.removeChild(leftSel.options[i]);
  139. //每次删除后要改变下标
  140. // i -= 1;
  141. i--;
  142. }
  143. }
  144. //人数
  145. leftAndRightCount();
  146. });
  147. //4、右边sel添加事件
  148. //5、给移除按钮添加事件
  149. //6、给全部添加按钮添加事件
  150. EventUtil.addHandler($("addAll_btn"), "click", function () {
  151. //遍历左边的options
  152. for (var i = 0; i < leftSel.options.length; i++) {
  153. //添加右边数据数组
  154. rightPersonArray.push(leftPersonArray[i]);
  155. //重置右边sel
  156. resetSel(rightSel, leftPersonArray[i].name);
  157. //删除左边数据数组
  158. leftPersonArray.splice(i, 1);
  159. //删除左边option
  160. leftSel.removeChild(leftSel.options[i]);
  161. //改变下标
  162. i--;
  163. }
  164. //人数
  165. leftAndRightCount();
  166. });
  167. //7、给全部移除按钮添加事件
  168. });
  169. //封装左边和右边人数的方法
  170. function leftAndRightCount () {
  171. $("left_label").innerHTML = leftPersonArray.length;
  172. $("right_label").innerHTML = rightPersonArray.length;
  173. }
  174. //封装重置sel的方法
  175. function resetSel (sel, textNodeValue) {
  176. //创建option
  177. var option = createElement("option");
  178. //创建textNode
  179. var optionValue = createTextNode(textNodeValue);
  180. //添加textNode
  181. option.appendChild(optionValue);
  182. //添加option
  183. sel.appendChild(option);
  184. }
  185. //封装创建元素的方法
  186. function createElement (elementName) {
  187. return document.createElement(elementName);
  188. }
  189. //封装创建textNode的方法
  190. function createTextNode (elementValue) {
  191. return document.createTextNode(elementValue);
  192. }
  193. //封装通过id获取标签的方法
  194. function $ (idName) {
  195. return document.getElementById(idName);
  196. }
  197. </script>
  198. </body>
  199. </html>

open()跨窗口传值


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>页面1</title>
  6. <script type="text/javascript">
  7. window.onload = function () {
  8. var oBtn = document.getElementById('btn1');
  9. oBtn.onclick = function () {
  10. window.open("open跨窗口传值2.html","123",'height=400,width=400,top=100,left=200,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');
  11. var content1 = document.getElementById('input1').value;
  12. }
  13. }
  14. </script>
  15. </head>
  16. <body>
  17. <input type="text" id="input1">
  18. <button id="btn1">点击</button>
  19. </body>
  20. </html>
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>页面2</title>
  6. <script type="text/javascript">
  7. window.onload = function () {
  8. /*var oBtn = document.getElementById('btn1');
  9. oBtn.onclick = function () {
  10. window.open("open跨窗口传值2.html");
  11. var content1 = document.getElementById('input1').value;
  12. }*/
  13. var content2 = document.getElementById("input2");
  14. content2.value= window.opener.document.getElementById('input1').value
  15. // 传回
  16. var oBtn = document.getElementById('btn2');
  17. oBtn.onclick = function () {
  18. window.opener.document.getElementById('input1').value = content2.value;
  19. }
  20. }
  21. </script>
  22. </head>
  23. <body>
  24. <input type="text" id="input2">
  25. <button id="btn2">点击</button>
  26. </body>
  27. </html>

代理设计模式


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>代理设计模式</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. /*
  10. 小明喜欢小红,想给小红送一朵花,但小明不好意思直接送,委托
  11. 小红的闺蜜小兰去送。最后小红接收到花。
  12. */
  13. // function Flower () {
  14. // }
  15. //构造函数
  16. var Flower = function (name) {
  17. this.name = name;
  18. }
  19. //小明
  20. var xiaoming = {
  21. sendFlower : function (target) {
  22. var flower = new Flower("菊花");
  23. //送给委托人小兰(最终通过委托人在送给小红)
  24. target.receive(flower);
  25. }
  26. }
  27. //小红
  28. var xiaohong = {
  29. //最后接花的人
  30. receive : function (flower) {
  31. console.log("接收到 " + flower.name + "了,好开心!");
  32. }
  33. }
  34. //小兰
  35. var xiaolan = {
  36. //接收花的方法,刚接完就送给小红
  37. receive : function (flower) {
  38. //给小红
  39. xiaohong.receive(flower);
  40. }
  41. }
  42. //小明送花
  43. xiaoming.sendFlower(xiaolan);
  44. </script>
  45. </body>
  46. </html>

单例设计模式


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>单例设计模式</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //人的构造函数
  10. var Person = function () {
  11. }
  12. //创建两个人的对象,证明是两个不同的实例
  13. var person1 = new Person();
  14. var person2 = new Person();
  15. console.log("person1 === person2:" + person1 === person2);
  16. // function fn () {
  17. // console.log("测试");
  18. // }
  19. //这种方式,声明即调用。
  20. //语法:(函数声明)();
  21. (function fn () {
  22. console.log("测试");
  23. })();
  24. //还可以这么写
  25. var fn1 = (function () {
  26. console.log("测试");
  27. })();
  28. //写一个单利
  29. var Dog = (function () {
  30. // console.log(123);
  31. console.log("==========");
  32. //通过闭包外部函数的局部变量,模拟“静态”变量,这个变量会存在内存中,每次调用外部函数的时候才初始化这个instance
  33. var instance;
  34. //这个才是真的构造函数实现
  35. var Dog = function (name) {
  36. console.log("---------");
  37. this.name = name;
  38. //判断instance是否为空
  39. if (instance) {
  40. return instance;
  41. }
  42. //如果不存在,把当前对象赋值给instance
  43. instance = this;
  44. return instance;
  45. }
  46. //外部函数return一个 内部的构造函数
  47. return Dog;
  48. })();
  49. var dog1 = new Dog("妞妞");
  50. var dog2 = new Dog("憨憨");
  51. console.log("dog1 === dog2 : " + (dog1 === dog2));
  52. //注意: 打印的是妞妞,因为还没有等到这个对象生成,就返回第一次那个对象了。
  53. console.log(dog2.name);
  54. dog2.name = "打脸";
  55. console.log(dog1.name);
  56. </script>
  57. </body>
  58. </html>

策略设计模式


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>策略设计模式</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //创建一个策略对象,方便管理相应的算法
  10. var stategaies = {
  11. a : function (salary) {
  12. return salary * 2;
  13. },
  14. b : function (salary) {
  15. return salary * 3;
  16. },
  17. c : function (salary) {
  18. return salary * 4;
  19. }
  20. }
  21. //
  22. var getMoney = function (level, salary) {
  23. //分配策略,因为是形参,通过中括号的语法调用属性,得到的是a、b、c方法,传入salary直接调用即可
  24. return stategaies[level](salary);
  25. }
  26. var money = getMoney("c", 20000);
  27. console.log(money);
  28. </script>
  29. </body>
  30. </html>

适配器设计模式


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>适配器设计模式01</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //开发调用外部接口的方式(没用适配器)
  10. var googleMap = {
  11. show : function () {
  12. console.log("显示谷歌地图数据!");
  13. }
  14. }
  15. var baiduMap = {
  16. show : function () {
  17. console.log("显示百度地图数据!");
  18. }
  19. }
  20. var renderMap = function (map) {
  21. //判断各种地图中是否有show函数
  22. if (map.show instanceof Function) {
  23. //如果有的话,就可以直接调用,显示数据
  24. map.show();
  25. }
  26. }
  27. renderMap(baiduMap);
  28. renderMap(googleMap);
  29. </script>
  30. </body>
  31. </html>
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>适配器设计模式02</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //利用适配器设计模式,解决谷歌和百度地图数据方法不兼容的问题
  10. var googleMap = {
  11. show : function () {
  12. console.log("显示谷歌地图!");
  13. }
  14. }
  15. var baiduMap = {
  16. loadData : function () {
  17. console.log("显示百度地图数据!");
  18. }
  19. }
  20. //给百度地图添加一个适配器对象
  21. var baiduAdapter = {
  22. //适配器实现一个show函数
  23. show : function () {
  24. //在这里调用百度显示数据的函数,并且把运行结果返回
  25. return baiduMap.loadData();
  26. }
  27. }
  28. var renderMap = function (map) {
  29. if (map.show instanceof Function) {
  30. map.show();
  31. }
  32. }
  33. renderMap(googleMap);
  34. // renderMap(baiduMap);
  35. //为了兼容,这里需要传入我们写的适配器对象
  36. renderMap(baiduAdapter);
  37. </script>
  38. </body>
  39. </html>

工厂设计模式


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>工厂设计模式</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //利用工厂设计模式创建对象
  10. function createPerson (name, age, sex) {
  11. var person = new Object();
  12. //动态插入了一个属性
  13. person.name = name;
  14. person.age = age;
  15. person.sex = sex;
  16. person.sayHello = function () {
  17. console.log(this.name);
  18. }
  19. return person;
  20. }
  21. var person1 = createPerson("小红", 18, "女");
  22. var person2 = createPerson("小明", 19, "男");
  23. person1.sayHello();
  24. person2.sayHello();
  25. console.log(person1 === person2);
  26. /*
  27. 工厂设计模式创建对象的优缺点
  28. 优点:解决了创建多个相似对象的问题。
  29. 缺点:没有解决对对象的识别问题(没有明确对象的类型)。
  30. */
  31. </script>
  32. </body>
  33. </html>

模板设计模式


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>模板设计模式</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. /*
  10. 咖啡
  11. 1、烧水
  12. 2、冲咖啡
  13. 3、把咖啡倒入杯子
  14. 4、加糖和奶
  15. */
  16. //喝咖啡的构造函数
  17. var Coffee = function () {
  18. }
  19. // function Coffee () {
  20. // }
  21. //1、烧水
  22. Coffee.prototype.boilWater = function () {
  23. console.log("烧水");
  24. }
  25. //2、冲咖啡
  26. Coffee.prototype.brewCoffee = function () {
  27. console.log("冲咖啡");
  28. }
  29. //3、把咖啡倒入杯子
  30. Coffee.prototype.pourInCup = function () {
  31. console.log("把咖啡倒入杯子");
  32. }
  33. //4、加糖、奶
  34. Coffee.prototype.addSugarAndMilk = function () {
  35. console.log("加糖和奶");
  36. }
  37. // 封装一下喝咖啡的流程
  38. Coffee.prototype.init = function () {
  39. this.boilWater();
  40. this.brewCoffee();
  41. this.pourInCup();
  42. this.addSugarAndMilk();
  43. }
  44. //创建一个咖啡对象
  45. var coffee = new Coffee();
  46. coffee.init();
  47. /*
  48. 1、烧水
  49. 2、沏茶
  50. 3、把茶放入杯子中
  51. 4、加柠檬
  52. */
  53. //喝茶的构造函数
  54. var Tea = function () {
  55. }
  56. //1、烧水
  57. Tea.prototype.boilWater = function () {
  58. console.log("烧水");
  59. }
  60. //2、沏茶
  61. Tea.prototype.brewTea = function () {
  62. console.log("沏茶");
  63. }
  64. //3、把茶放到杯子中
  65. Tea.prototype.pourInCup = function () {
  66. console.log("把茶放到杯子中");
  67. }
  68. //4、加柠檬
  69. Tea.prototype.addLemon = function () {
  70. console.log("加柠檬");
  71. }
  72. //封装喝茶的流程
  73. Tea.prototype.init = function () {
  74. this.boilWater();
  75. this.brewTea();
  76. this.pourInCup();
  77. this.addLemon();
  78. }
  79. Tea.prototype = new S();
  80. //创建茶的对象
  81. var tea = new Tea();
  82. tea.init();
  83. </script>
  84. </body>
  85. </html>
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>模板设计模式02</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. /*
  10. 经过分析,我们可以把某些构造函数,相同的部分抽象出来,抽象到一个父类(构造函数)里,那么其他子类(构造函数)的原型对象只要指向这个父类(构造函数)的实例,就形成了一个继承关系。切记:JS中没有类的继承,只有对象的继承。换句话说,某个类型只能继承于某个类型的一个实例对象。
  11. 之前写的咖啡和茶的流程,我们可以抽象出来以下几个步骤:
  12. 1、烧水
  13. 2、冲(饮品)名字可以叫做:brew()
  14. 3、把(饮品)放到杯子中
  15. 4、加(材料)名字可以叫做:addDrink()
  16. */
  17. //父类(构造函数)
  18. var SuperObj = function () {
  19. }
  20. //抽象出来的烧水方法
  21. SuperObj.prototype.boilWater = function () {
  22. //这个要实现,因为无论喝的是什么饮品,都需要烧水,它们(子类)都具备相同的烧水功能,所以在父类里实现这个方法。子类就不用实现了,直接调用即可。
  23. console.log("烧水!");
  24. }
  25. //抽象出来的冲(饮品)方法
  26. SuperObj.prototype.brew = function () {
  27. //这里什么都不用实现,让子类去实现它们想要实现的功能,
  28. //因为冲什么父类也不知道。
  29. }
  30. //抽象出来的把饮品放在杯子中
  31. SuperObj.prototype.pourInCup = function () {
  32. //这里也是什么都不用实现,因为不确定放的是什么饮品。
  33. }
  34. //抽象出来的加材料的方法
  35. SuperObj.prototype.addDrink = function () {
  36. //这里也什么都不用实现,因为不确定加的是什么材料
  37. }
  38. //抽象出来的喝饮品流程的方法
  39. SuperObj.prototype.init = function () {
  40. //要实现这个方法,因为无论是咖啡还是茶,都是以上者四个流程,
  41. //只是流程内部的实现不一样而已。
  42. this.boilWater();
  43. this.brew();
  44. this.pourInCup();
  45. this.addDrink();
  46. }
  47. //咖啡构造函数
  48. var Coffee = function () {
  49. }
  50. //建立子类和父类的关系(也就是指定一下原型对象)
  51. Coffee.prototype = new SuperObj();
  52. //父类烧水的方法可以满足当前子类,所以不用重写。
  53. //重写:重写的意思就是,重新实现一下父类的某个方法,相当于覆盖掉了父类的这个方法。重写的方法名一定要和父类你要覆盖的那个方法名一致,参数也一模一样。
  54. //重写冲饮品的方法
  55. Coffee.prototype.brew = function () {
  56. console.log("准备冲咖啡!");
  57. }
  58. //重写把饮品放到杯子中的方法
  59. Coffee.prototype.pourInCup = function () {
  60. console.log("把咖啡倒入杯子中!");
  61. }
  62. //重写加材料的方法
  63. Coffee.prototype.addDrink = function () {
  64. console.log("给咖啡加糖和牛奶!");
  65. }
  66. //创建一个咖啡对象
  67. var coffee1 = new Coffee();
  68. coffee1.init();
  69. //茶的构造函数
  70. var Tea = function () {
  71. }
  72. //子类和父类建立联系(设置原型对象)
  73. Tea.prototype = new SuperObj();
  74. //重写冲饮品的方法
  75. Tea.prototype.brew = function () {
  76. console.log("沏茶!");
  77. }
  78. //重写把饮品放到杯子中的方法
  79. Tea.prototype.pourInCup = function () {
  80. console.log("把茶倒入杯子中!");
  81. }
  82. //重写加材料的方法
  83. Tea.prototype.addDrink = function () {
  84. console.log("给茶添加柠檬!");
  85. }
  86. //创建tea对象
  87. var tea1 = new Tea();
  88. tea1.init();
  89. </script>
  90. </body>
  91. </html>

观察者模式


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>观察者设计模式缺陷版</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. /*
  10. 场景:小明、小红等人去售楼处买房,但没有中意的房子,留了自己的联系方式给售楼处,然后将来有合适的房源后,会发短信通知小明或小红等人。
  11. 发布者:售楼处
  12. 订阅者:小红、小明等人
  13. - 售楼处收集或存储小红、小明等人的信息(回调函数)
  14. - 售楼处发短信通知小红、小明等人来看房。
  15. */
  16. //售楼处对象 (发布者)
  17. var saleHouse = {
  18. };
  19. //用户信息列表(订阅者)
  20. saleHouse.clientList = [];
  21. //添加用户信息的方法 (回调函数)
  22. //参数是一个回调函数
  23. saleHouse.listen = function (callBackFn) {
  24. //把函数添加到用户信息列表中
  25. this.clientList.push(callBackFn);
  26. }
  27. //通知方法
  28. saleHouse.trigger = function () {
  29. for (var i = 0; i < this.clientList.length; i++) {
  30. // this.clientList[i]();
  31. //调用函数 (就是获取信息列表里存取的回调函数,然后进行调用)
  32. this.clientList[i].apply(this, arguments);
  33. }
  34. }
  35. //调用添加用户信息方法
  36. //可以理解为这个是注册小明的用户信息
  37. saleHouse.listen(function (houseName, price) {
  38. console.log("房名:" + houseName);
  39. console.log("价格:" + price);
  40. });
  41. //在添加一个小红
  42. saleHouse.listen(function (houseName, price) {
  43. console.log("房名:" + houseName);
  44. console.log("价格:" + price);
  45. });
  46. // console.log(saleHouse.clientList); //
  47. //通知小明和小红房源信息
  48. saleHouse.trigger('英国宫', 1100000);
  49. console.log("-----------------");
  50. saleHouse.trigger('剑桥郡', 1300000);
  51. </script>
  52. </body>
  53. </html>
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>观察者设计模式_改进版</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //售楼处 发布者
  10. var saleHouse = {
  11. }
  12. //用户信息列表(Map中的值是用户信息列表,键是相应的订阅者) (订阅者)
  13. saleHouse.clientMap = new Map();
  14. //添加用户信息的方法 (回调函数)
  15. //参数是一个回调函数 (带标示)
  16. saleHouse.listen = function (key, callBackFn) {
  17. //先判断用户信息Map中是否有这个key
  18. if (!saleHouse.clientMap.get(key)) {
  19. //如果没有当前key,则加入key并且初始化key对应的value,value是一个回调函数数组
  20. saleHouse.clientMap.set(key, []);
  21. }
  22. //把传进来的回调函数push到Map中key对应的value数组中
  23. saleHouse.clientMap.get(key).push(callBackFn);
  24. }
  25. //通知方法
  26. saleHouse.trigger = function () {
  27. //获取传进来参数数组中的第一个参数,也就是订阅者Map中的key
  28. var key = arguments[0];
  29. // Array.prototype.shift.call(this,arguments);
  30. //获得该标识下的回调函数数组
  31. var callBackFns = this.clientMap.get(key);
  32. //判断数组是否为空
  33. if (!callBackFns || callBackFns.length == 0) {
  34. return;
  35. }
  36. //遍历当前key下对应的value函数数组,并且调用
  37. for (var i = 0; i < callBackFns.length; i++) {
  38. callBackFns[i].call(this, arguments[1], arguments[2]);
  39. }
  40. }
  41. //调用listen方法(添加小明)
  42. saleHouse.listen('小明' ,function (houseName, price) {
  43. console.log('小明');
  44. console.log('房子:' + houseName);
  45. console.log('价格:' + price);
  46. });
  47. //调用listen方法(添加小红)
  48. saleHouse.listen('小红' ,function (houseName, price) {
  49. console.log('小红');
  50. console.log('房子:' + houseName);
  51. console.log('价格:' + price);
  52. });
  53. //调用通知方法
  54. saleHouse.trigger('小明','英国宫', 2000000);
  55. saleHouse.trigger('小明','剑桥郡', 3000000);
  56. saleHouse.trigger('小红','英国宫', 2000000);
  57. </script>
  58. </body>
  59. </html>
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>观察者模式最终完美版</title>
  6. <script src="../../../../../jquery-2.2.3.js"></script>
  7. <script>
  8. /*
  9. 发布者: 售楼处
  10. 订阅者: 小明小红等人
  11. 开发思路:
  12. 1. 售楼处存储小红,小明等人的信息(用到回调函数的概念)
  13. 2. 售楼处发短信通知小明和小红来看房
  14. */
  15. /*
  16. 取消订阅的功能
  17. 售楼处2不能用售楼处的方法
  18. */
  19. // 观察者设计模式通用模板对象
  20. var eventObj = {
  21. clientMap : new Map(),
  22. // 订阅方法
  23. listen : function (key, callback) {
  24. if (!this.clientMap.get(key)) {
  25. this.clientMap.set(key, []);
  26. }
  27. // callback 添加到key对应的value数组中
  28. this.clientMap.get(key).push(callback);
  29. },
  30. // 取消订阅方法
  31. remove : function (key) {
  32. if (!this.clientMap.get(key)) {
  33. alert("无此人");
  34. return;
  35. }
  36. /*
  37. 数组的方法:
  38. slice是截取子数组
  39. slice(start, end)
  40. splice 是 删,改,增
  41. splice(start, length, 替换1, 替换2, 替换3,.....)
  42. split: 是字符串的方法
  43. 字符串中没有splice方法
  44. */
  45. // 方法一:
  46. // key有,value中数组为空
  47. this.clientMap.get(key).splice(0);
  48. // 方法二:
  49. // key有,value中数组为空
  50. //this.clientMap.set(key,[]);
  51. // 方法三:
  52. // 删除键和值
  53. //this.clientMap.delete(key);
  54. console.log("已经取消订阅");
  55. },
  56. // 通知方法
  57. trigger : function () {
  58. var key = arguments[0];
  59. var callbackFns = this.clientMap.get(key);
  60. if(!callbackFns || callbackFns.length === 0) {
  61. return;
  62. }
  63. for (var i = 0; i< callbackFns.length; i++) {
  64. callbackFns[i].call(this, arguments[1],arguments[2]);
  65. }
  66. // 测试
  67. console.log(callbackFns.length);
  68. }
  69. };
  70. // 复制工厂 , 调用这个方法后,对象就拥有了模板里的所有属性
  71. // 用来渲染属性
  72. var installEvent = function (obj) {
  73. // 把模板对象的指针赋值给了obj
  74. // obj 也指向了eventObj对象
  75. // obj = eventObj; // 不能直接赋值
  76. for (var tempProp in eventObj) {
  77. // obj[tempProp] 给obj添加了一个新的tempProp属性
  78. obj[tempProp] = eventObj[tempProp];
  79. }
  80. //obj['clientMap'] = new Map();
  81. obj.clientMap = new Map();
  82. };
  83. // 售楼处1
  84. var saleHouse1 = {};
  85. // 售楼处2
  86. var saleHouse2 = {};
  87. // 渲染属性
  88. installEvent(saleHouse1);
  89. installEvent(saleHouse2);
  90. // 复制过了, 售楼处已经有属性了
  91. // 添加用户信息方法
  92. // 调用listen方法 (添加小明)
  93. saleHouse1.listen('小明',function(houseName, price) {
  94. console.log('小明');
  95. console.log("房名:" +houseName);
  96. console.log("价格:" +price);
  97. });
  98. saleHouse2.listen('小明',function(houseName, price) {
  99. console.log('小明');
  100. console.log("房名:" +houseName);
  101. console.log("价格:" +price);
  102. });
  103. // 调用listen方法 (添加小红)
  104. saleHouse1.listen('小红',function(houseName, price) {
  105. console.log('小红');
  106. console.log("房名:" +houseName);
  107. console.log("价格:" +price);
  108. });
  109. saleHouse2.listen('小红',function(houseName, price) {
  110. console.log('小红');
  111. console.log("房名:" +houseName);
  112. console.log("价格:" +price);
  113. });
  114. // 小明取消订阅
  115. saleHouse1.remove("小明");
  116. // 通知
  117. // 通知小明和小红 (群发)
  118. saleHouse1.trigger('小明','英国宫_售楼处1',110000);
  119. saleHouse2.trigger('小明','英国宫_售楼处2',110000);
  120. console.log("*******");
  121. saleHouse1.trigger('小红','剑桥郡_售楼处1',130000);
  122. saleHouse2.trigger('小红','剑桥郡_售楼处2',130000);
  123. </script>
  124. </head>
  125. <body>
  126. </body>
  127. </html>

高级函数

作用域安全问题


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>高阶函数</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //高阶函数 (函数可以作为参数、函数可以赋值给变量、函数可以作为返回值)
  10. //1、事件的封装
  11. // .....
  12. //2、Ajax的封装
  13. //......
  14. //作用域安全问题
  15. //猫的构造器
  16. var Cat = function (name, type) {
  17. this.name = name;
  18. this.type = type;
  19. }
  20. //创建猫的对象
  21. var cat1 = new Cat("端午", "波斯猫");
  22. console.log(cat1.name, cat1.type);
  23. //这只是一个普通函数调用
  24. var cat2 = Cat("咪咪", "加菲猫");
  25. // console.log(cat2.name, cat2.type);
  26. //如果直接调用Cat函数的话,内部的this指针,就会根据当前运行环境改变而变成window
  27. console.log(window.name, window.type);
  28. //解决上面的问题。
  29. //狗的构造函数
  30. var Dog = function (name, type) {
  31. //判断this指针的构造函数是否是Dog
  32. if (this instanceof Dog) {
  33. this.name = name;
  34. this.type = type;
  35. //返回当前对象
  36. return this;
  37. }
  38. return new Dog(name, type);
  39. }
  40. //正常通过new 关键字创建狗的对象
  41. var dog1 = new Dog("妞妞", "萨摩耶");
  42. console.log(dog1.name, dog1.type);
  43. //直接调用Dog() 创建狗的对象
  44. var dog2 = Dog("八公", "秋田犬");
  45. console.log(dog2.name, dog2.type);
  46. console.log(window.name, window.type);
  47. </script>
  48. </body>
  49. </html>

函数的柯里化

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>柯里化</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //全局变量实现叠加功能
  10. //声明全局变量
  11. var totalCount = 0;
  12. var fn = function (count) {
  13. totalCount += count;
  14. }
  15. //每次调用fn
  16. fn(100);
  17. fn(200);
  18. fn(300);
  19. console.log(totalCount);
  20. //利用柯里化可以实现,把每次执行函数的结果存储起来,当你最后想得到结果的时候在停止存储。
  21. var fn2 = (function () {
  22. //把每次调用内部函数的结果都存在这个数组中,因为是闭包,这个数组不会被销毁,存在内存中。
  23. var args = [];
  24. return function () {
  25. //当没有参数传入的时候,停止,得到最后的结果
  26. if (!arguments.length) {
  27. //!arguments.length 和 arguments.length == 0 是一个意思
  28. var returnNum = 0;
  29. //遍历args取出每个结果,叠加起来,返回。
  30. for (var i = 0; i < args.length; i++) {
  31. returnNum += args[i];
  32. }
  33. return returnNum;
  34. }
  35. //把每次传进来的数值存储到args数组中
  36. // args.push(arguments[0]);
  37. [].push.apply(args, arguments);
  38. }
  39. })();
  40. fn2(100);
  41. fn2(300);
  42. fn2(600);
  43. //当函数没有传入任何参数的时候停止。得到最后的结果。
  44. console.log(fn2());
  45. </script>
  46. </body>
  47. </html>

定时器


setTimeOut

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>柯里化</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //全局变量实现叠加功能
  10. //声明全局变量
  11. var totalCount = 0;
  12. var fn = function (count) {
  13. totalCount += count;
  14. }
  15. //每次调用fn
  16. fn(100);
  17. fn(200);
  18. fn(300);
  19. console.log(totalCount);
  20. //利用柯里化可以实现,把每次执行函数的结果存储起来,当你最后想得到结果的时候在停止存储。
  21. var fn2 = (function () {
  22. //把每次调用内部函数的结果都存在这个数组中,因为是闭包,这个数组不会被销毁,存在内存中。
  23. var args = [];
  24. return function () {
  25. //当没有参数传入的时候,停止,得到最后的结果
  26. if (!arguments.length) {
  27. //!arguments.length 和 arguments.length == 0 是一个意思
  28. var returnNum = 0;
  29. //遍历args取出每个结果,叠加起来,返回。
  30. for (var i = 0; i < args.length; i++) {
  31. returnNum += args[i];
  32. }
  33. return returnNum;
  34. }
  35. //把每次传进来的数值存储到args数组中
  36. // args.push(arguments[0]);
  37. [].push.apply(args, arguments);
  38. }
  39. })();
  40. fn2(100);
  41. fn2(300);
  42. fn2(600);
  43. //当函数没有传入任何参数的时候停止。得到最后的结果。
  44. console.log(fn2());
  45. </script>
  46. </body>
  47. </html>

setInterval()

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>setInterval的使用</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //setInterval的使用
  10. setInterval(function () {
  11. console.log("a");
  12. }, 2000);
  13. //带参数
  14. setInterval(function (a, b) {
  15. console.log(a + b);
  16. }, 2000, 5, 5);
  17. //利用setTimeout()实现setInterval()的功能
  18. var mySetInterval = function (fn, wait) {
  19. var tempSetInterval = function () {
  20. //fn指向全局
  21. fn.call();
  22. setTimeout(tempSetInterval, wait);
  23. }
  24. //第一次的时候调用
  25. setTimeout(tempSetInterval, wait);
  26. }
  27. mySetInterval(function () {
  28. console.log("自己实现的setInterval");
  29. }, 1000);
  30. //setTimeout()函数和setInterval()函数的返回值,是一个整数类型连续的值(在正常的环境下)。
  31. //换句话说,第二个开启的setTimeout函数的返回值,要比第一个开启的值大。
  32. (function () {
  33. //0表示没有间隔,gid是返回值。用于销毁定时器
  34. var gid = setInterval(clearAllTimer, 0);
  35. //清空所有定时器
  36. function clearAllTimer () {
  37. var id = setTimeout(function () {}, 0);
  38. while (id > 0) {
  39. //除了外面轮训的gid以外 全部清空
  40. // if (id !== gid) {
  41. clearTimeout(id);
  42. // }
  43. id--;
  44. }
  45. }
  46. })();
  47. </script>
  48. </body>
  49. </html>

飞翔的小球和弹弹球

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>飞翔的小球和弹弹球</title>
  6. </head>
  7. <body>
  8. <div id="left_div" style="width:500px;height:500px;position:absolute;background:red;">
  9. <div id="left_fly_div" style="width:50px;height:50px;border-radius:25px;background:green;position:absolute;">
  10. </div>
  11. </div>
  12. <div id="right_div" style="width:500px;height:500px;position:relative;float:right;background:blue;">
  13. <div id="right_fly_div" style="width:50px;height:50px;border-radius:25px;background:red;position:absolute;" >
  14. </div>
  15. </div>
  16. <script type="text/javascript">
  17. //飞翔的小球
  18. //获得左边的小球div
  19. var leftFlyDiv = document.getElementById("left_fly_div");
  20. //飞翔差值初始化
  21. var frameX = 0;
  22. var frameY = 0;
  23. //开启定时器
  24. var leftTimerId = setInterval(function () {
  25. //改变差值
  26. frameX += 1;
  27. frameY += 1;
  28. //改变小球的left
  29. leftFlyDiv.style.left = frameX + 'px';
  30. //改变小球的top
  31. leftFlyDiv.style.top = frameY + 'px';
  32. }, 10);
  33. //弹弹球
  34. //获得右边小球的div
  35. var rightFlyDiv = document.getElementById("right_fly_div");
  36. //获得右边外部的div
  37. var rightDiv = document.getElementById("right_div");
  38. //初始化右边小球的left和top
  39. rightFlyDiv.style.left = "0px";
  40. rightFlyDiv.style.top = "0px";
  41. //弹弹球的差值
  42. var rightFrameX = 5;
  43. var rightFrameY = 2;
  44. // alert(rightFlyDiv.style.left);
  45. // alert(rightFlyDiv.style.width);
  46. // alert(parseInt("0px"));
  47. //开启轮询定时器
  48. var rightTimer = setInterval(function () {
  49. //先计算每次飞多少left 和 top
  50. rightFlyDiv.style.left = parseInt(rightFlyDiv.style.left) + rightFrameX + 'px';
  51. rightFlyDiv.style.top = parseInt(rightFlyDiv.style.top) + rightFrameY + 'px';
  52. //判断什么时候反向弹
  53. //左右
  54. if (parseInt(rightFlyDiv.style.left) + parseInt(rightFlyDiv.style.width) >= parseInt(rightDiv.style.width) || parseInt(rightFlyDiv.style.left) <= 0) {
  55. //改变差值
  56. rightFrameX *= -1;
  57. }
  58. //上下
  59. if (parseInt(rightFlyDiv.style.top) + parseInt(rightFlyDiv.style.height) >= parseInt(rightDiv.style.height) || parseInt(rightFlyDiv.style.top) <= 0) {
  60. //改变差值
  61. rightFrameY *= -1;
  62. }
  63. }, 10);
  64. </script>
  65. </body>
  66. </html>
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>飞翔的小球和弹弹球</title>
  6. </head>
  7. <body>
  8. <div id="left_div" style="width:500px;height:500px;position:absolute;background:red;">
  9. <div id="left_fly_div" style="width:50px;height:50px;border-radius:25px;background:green;position:absolute;">
  10. </div>
  11. </div>
  12. <div style="float:left; width:100px;height:100px;margin-left:600px;" >
  13. <input type="button" value="开始" id="my_btn" style="width:100px;height:100px;font-size:28px;"/>
  14. </div>
  15. <div id="right_div" style="width:500px;height:500px;position:relative;float:right;background:blue;">
  16. <div id="right_fly_div" style="width:50px;height:50px;border-radius:25px;background:red;position:absolute;" >
  17. </div>
  18. </div>
  19. <script type="text/javascript">
  20. //飞翔的小球
  21. //获得左边的小球div
  22. var leftFlyDiv = document.getElementById("left_fly_div");
  23. //飞翔差值初始化
  24. var frameX = 0;
  25. var frameY = 0;
  26. //开启定时器
  27. var leftTimerId = setInterval(function () {
  28. //改变差值
  29. frameX += 1;
  30. frameY += 1;
  31. //改变小球的left
  32. leftFlyDiv.style.left = frameX + 'px';
  33. //改变小球的top
  34. leftFlyDiv.style.top = frameY + 'px';
  35. }, 10);
  36. var myFn = (function () {
  37. //定时器返回的timerId
  38. var rightTimer;
  39. //弹弹球
  40. //获得右边外部的div
  41. var rightDiv = document.getElementById("right_div");
  42. //获得右边小球的div
  43. var rightFlyDiv = document.getElementById("right_fly_div");
  44. //初始化右边小球的left和top
  45. rightFlyDiv.style.left = "0px";
  46. rightFlyDiv.style.top = "0px";
  47. //弹弹球的差值
  48. var rightFrameX = 5;
  49. var rightFrameY = 2;
  50. return function (flag) {
  51. //如果点击停止就清除定时器
  52. if (!flag) {
  53. clearInterval(rightTimer);
  54. return;
  55. }
  56. // alert(rightFlyDiv.style.left);
  57. // alert(rightFlyDiv.style.width);
  58. // alert(parseInt("0px"));
  59. //开启轮询定时器
  60. rightTimer = setInterval(function () {
  61. //先计算每次飞多少left 和 top
  62. rightFlyDiv.style.left = parseInt(rightFlyDiv.style.left) + rightFrameX + 'px';
  63. rightFlyDiv.style.top = parseInt(rightFlyDiv.style.top) + rightFrameY + 'px';
  64. //判断什么时候反向弹
  65. //左右
  66. if (parseInt(rightFlyDiv.style.left) + parseInt(rightFlyDiv.style.width) >= parseInt(rightDiv.style.width) || parseInt(rightFlyDiv.style.left) <= 0) {
  67. //改变差值
  68. rightFrameX *= -1;
  69. }
  70. //上下
  71. if (parseInt(rightFlyDiv.style.top) + parseInt(rightFlyDiv.style.height) >= parseInt(rightDiv.style.height) || parseInt(rightFlyDiv.style.top) <= 0) {
  72. //改变差值
  73. rightFrameY *= -1;
  74. }
  75. }, 10);
  76. }
  77. })();
  78. //按钮点击事件
  79. var myBtn = document.getElementById("my_btn");
  80. myBtn.onclick = function () {
  81. if (this.value == '开始') {
  82. this.value = '停止';
  83. myFn(true);
  84. } else {
  85. this.value = '开始';
  86. myFn(false);
  87. }
  88. }
  89. </script>
  90. </body>
  91. </html>

div透明度渐变

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>div透明度渐变</title>
  6. </head>
  7. <body>
  8. <div id="my_div" style="width:500px;height:500px;background:red;float:left;">
  9. </div>
  10. <div id="right_div" style="width:500px;height:500px;background:blue;float:right;">
  11. </div>
  12. <script type="text/javascript">
  13. //实现渐隐功能
  14. //获取要渐隐的div
  15. var myDiv = document.getElementById("my_div");
  16. //透明度初始值
  17. var opacity = 1;
  18. var timerId = setInterval(function () {
  19. //每次轮训都把透明度的变量减少0.05
  20. opacity -= 0.1;
  21. if (opacity > 0) {
  22. //改变myDiv的透明度
  23. myDiv.style.opacity = opacity;
  24. } else {
  25. //手动变成0
  26. // myDiv.style.opacity = 0;
  27. //透明度没了之后,清空定时器
  28. clearInterval(timerId);
  29. }
  30. }, 100);
  31. //获取右边div
  32. var rightDiv = document.getElementById("right_div");
  33. //手动初始化一下透明度样式,不初始化,不能直接调用
  34. rightDiv.style.opacity = 1;
  35. //渐变差值
  36. var rightOpacity = 0.1;
  37. var rightTimerId = setInterval(function () {
  38. //透明度每次改变
  39. rightDiv.style.opacity -= rightOpacity;
  40. if (rightDiv.style.opacity >= 1) {
  41. rightOpacity *= -1;
  42. } else if (rightDiv.style.opacity <= 0) {
  43. rightOpacity *= -1;
  44. }
  45. }, 100);
  46. </script>
  47. </body>
  48. </html>

js中调用css中的属性遇到的问题


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>JS调用CSS遇到的问题</title>
  6. <style type="text/css">
  7. #your_div {
  8. width: 200px;
  9. height: 200px;
  10. background: blue;
  11. }
  12. </style>
  13. </head>asdgghffj,l.kgv
  14. <body>
  15. <div id="my_div" style="width:200px;height:200px;background:red">
  16. </div>
  17. <div id="your_div">
  18. </div>
  19. <script type="text/javascript">
  20. //测试
  21. //获得两个div
  22. var myDiv = document.getElementById("my_div");
  23. var yourDiv = document.getElementById("your_div");
  24. //通过style属性获得它们相应的样式
  25. console.log("myDiv = " + myDiv.style.width);
  26. console.log("yourDiv = " +yourDiv.style.width);
  27. //给yourDiv设值样式值
  28. yourDiv.style.width = "200px";
  29. console.log("yourDiv = " + yourDiv.style.width);
  30. //通过getComputedStyle()函数来获取yourDiv的高度,
  31. //不需要提前赋值就可以获取到
  32. console.log("yourDiv = " + getComputedStyle(yourDiv, null).height);
  33. //调用封装后的获取样式值
  34. console.log("yourDiv = " + getStyle(yourDiv, "height"));
  35. //封装一个通过样式属性名获取值得函数
  36. function getStyle (element, attr) {
  37. if (window.getComputedStyle) {
  38. return getComputedStyle(element, null)[attr];
  39. } else if (window.currentStyle) {
  40. return element.currentStyle[attr];
  41. } else {
  42. return element.style[attr];
  43. }
  44. }
  45. </script>
  46. </body>
  47. </html>

鼠标点击出现div


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>鼠标点击出现DIV</title>
  6. <style type="text/css">
  7. #out_div {
  8. width: 500px;
  9. height: 500px;
  10. background: red;
  11. position: relative;
  12. }
  13. #in_div {
  14. width: 100px;
  15. height: 100px;
  16. position: absolute;
  17. background: yellow;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="out_div">
  23. <div id="in_div">
  24. </div>
  25. </div>
  26. <script type="text/javascript">
  27. //获得最后div要移动坐标的函数
  28. function getPosition (event) {
  29. var x = event.clientX; //获得鼠标当前在浏览器中的x坐标
  30. var y = event.clientY; //获得鼠标当前在浏览器中的y坐标
  31. var r = event.target.getBoundingClientRect(); //获取在浏览器中被点击元素的范围信息
  32. //鼠标最后点击的x、y的位置
  33. x -= r.left;
  34. y -= r.top;
  35. //把最后的x、y 封装到一个对象中 返回。
  36. var positionObj = {
  37. m_x : x,
  38. m_y : y
  39. }
  40. return positionObj;
  41. }
  42. //给out_div添加单击事件
  43. var outDiv = document.getElementById("out_div");
  44. outDiv.onclick = function (event) {
  45. //获得in_div
  46. var inDiv = document.getElementById("in_div");
  47. //判断是否点击在了inDiv上
  48. if (event.target === inDiv) {
  49. return;
  50. }
  51. //否则改变inDiv的位置
  52. var positionObj = getPosition(event);
  53. inDiv.style.left = positionObj.m_x + "px";
  54. inDiv.style.top = positionObj.m_y + "px";
  55. }
  56. </script>
  57. </body>
  58. </html>

iphone秒表


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>ipone秒表</title>
  6. <style type="text/css">
  7. #out_div{
  8. width: 350px;
  9. height: 500px;
  10. margin: 0 auto;
  11. background: #ccc;
  12. }
  13. #top_div {
  14. width: 350px;
  15. height: 200px;
  16. background: red;
  17. }
  18. #center_div{
  19. width: 350px;
  20. height: 100px;
  21. background: green;
  22. }
  23. #bottom_div {
  24. width: 350px;
  25. height: 200px;
  26. background: orange;
  27. }
  28. h3{
  29. text-align: center;
  30. line-height: 40px;
  31. }
  32. #small_span {
  33. display: block;
  34. height: 50px;
  35. text-align: right;
  36. font-size: 35px;
  37. margin-right: 20px;
  38. }
  39. #big_span {
  40. display: block;
  41. height: 50px;
  42. text-align: right;
  43. font-size: 60px;
  44. margin-right: 20px;
  45. }
  46. #left_btn {
  47. width: 50px;
  48. height: 50px;
  49. margin-top: 15px;
  50. margin-left: 125px;
  51. font-size: 20px;
  52. }
  53. #right_btn {
  54. width: 50px;
  55. height: 50px;
  56. margin-top: 15px;
  57. font-size: 20px;
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <div id="out_div">
  63. <div id="top_div">
  64. <h3>秒表</h3>
  65. <hr />
  66. <span id="small_span">00:00.00</span>
  67. <span id="big_span">00:00.00</span>
  68. </div>
  69. <div id="center_div">
  70. <input type="button" value="计次" id="left_btn" disabled />
  71. <input type="button" value="启动" id="right_btn"/>
  72. </div>
  73. <div id="bottom_div">
  74. </div>
  75. </div>
  76. <script type="text/javascript">
  77. //秒表的实现
  78. var timerfn = (function () {
  79. //定时器的返回值
  80. var timerId;
  81. //获取两个时间文本
  82. var smallSpan = $("small_span");
  83. var bigSpan = $("big_span");
  84. var smallM = 0; //分
  85. var smallS = 0; //秒
  86. var smallMS = 0; //毫秒
  87. var smallMMSS = 0; //微秒
  88. var bigM = 0; //分 (大)
  89. var bigS = 0; //秒
  90. var bigMS = 0; //毫秒
  91. var bigMMSS = 0; //微秒
  92. return function (flag, type) {
  93. if (!flag) {
  94. if (type == '复位') {
  95. //清空small的文本为0
  96. smallMMSS = 0;
  97. smallMS = 0;
  98. smallS = 0;
  99. smallM = 0;
  100. //清空big的文本为0
  101. bigMMSS = 0;
  102. bigMS = 0;
  103. bigS = 0;
  104. bigM = 0;
  105. //文本也清空
  106. smallSpan.innerHTML = "00:00.00";
  107. bigSpan.innerHTML = "00:00.00";
  108. }
  109. clearInterval(timerId);
  110. return;
  111. }
  112. //如果当前type是计次
  113. if (type == '计次') {
  114. //清空small的文本为0
  115. smallMMSS = 0;
  116. smallMS = 0;
  117. smallS = 0;
  118. smallM = 0;
  119. //初不初始化都行。
  120. // smallSpan.innerHTML = "00:00.00";
  121. }
  122. timerId = setInterval(function () {
  123. //小的时间递增
  124. smallMMSS++;
  125. if (smallMMSS == 10) {
  126. smallMMSS = 0;
  127. smallMS++;
  128. if (smallMS == 10) {
  129. smallMS = 0;
  130. smallS++;
  131. if (smallS == 60) {
  132. smallS = 0;
  133. smallM++;
  134. //..
  135. }
  136. }
  137. }
  138. //大的时间递增
  139. bigMMSS++;
  140. if (bigMMSS == 10) {
  141. bigMMSS = 0;
  142. bigMS++;
  143. if (bigMS == 10) {
  144. bigMS = 0;
  145. bigS++;
  146. if (bigS == 60) {
  147. bigS = 0;
  148. bigM++;
  149. //..
  150. }
  151. }
  152. }
  153. //赋值
  154. smallSpan.innerHTML = smallM + ":" + smallS + "." + smallMS + smallMMSS;
  155. bigSpan.innerHTML = bigM + ":" + bigS + "." + bigMS + bigMMSS;
  156. }, 10);
  157. }
  158. })();
  159. //右边按钮的事件
  160. $("right_btn").onclick = function () {
  161. if (this.value == '启动') {
  162. this.value = '停止';
  163. //左边按钮变计次
  164. $("left_btn").value = '计次';
  165. //左边按钮解禁
  166. $("left_btn").disabled = null;
  167. timerfn(true);
  168. } else {
  169. this.value = '启动';
  170. //左边按钮变成复位
  171. $("left_btn").value = '复位';
  172. timerfn(false);
  173. }
  174. }
  175. //左边按钮事件
  176. $("left_btn").onclick = function () {
  177. if (this.value == '计次') {
  178. //先清空,在启动
  179. timerfn(false, this.value);
  180. timerfn(true, this.value);
  181. } else {
  182. //清掉所有数据,并且暂停
  183. timerfn(false, this.value);
  184. this.value = '计次';
  185. //还要变成禁用的
  186. this.disabled = "disabled";
  187. }
  188. }
  189. //封装通过ID获得元素的函数
  190. function $ (idName) {
  191. return document.getElementById(idName);
  192. }
  193. </script>
  194. </body>
  195. </html>

Map


Map的理解

  1. 4.3 映射与集合
  2. Map 类型,也称为简单映射,只有一个目的:保存一组键值对儿。开发人员通常都使用普通对象来
  3. 保存键值对儿, 但问题是那样做会导致键容易与原生属性混淆。 简单映射能做到键和值与对象属性分离,
  4. 从而保证对象属性的安全存储。以下是使用简单映射的几个例子。
  5. var map = new Map();
  6. map.set("name", "Nicholas");
  7. map.set("book", "Professional JavaScript");
  8. console.log(map.has("name")); //true
  9. console.log(map.get("name")); //"Nicholas"
  10. map.delete("name");
  11. 简单映射的基本 API 包括 get() set() delete() ,每个方法的作用看名字就知道了。键可以
  12. 是原始值,也可是引用值。
  13. 与简单映射相关的是 Set 类型。 集合就是一组不重复的元素。 与简单映射不同的是, 集合中只有键,
  14. 没有与键关联的值。在集合中,添加元素要使用 add() 方法,检查元素是否存在要使用 has() 方法,而
  15. 删除元素要使用 delete() 方法。以下是基本的使用示例。
  16. var set = new Set();
  17. set.add("name");
  18. console.log(set.has("name")); //true
  19. set.delete("name");
  20. console.log(set.has("name")); //false

Map简单的应用

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Map的简单使用</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //创建一个map对象
  10. var map = new Map();
  11. //给map添加键值对
  12. map.set("a", "刘升升");
  13. map.set("b", "王沫沫");
  14. map.set("d", {name : "张晓晓", age : 18});
  15. map.set("c", 18);
  16. //添加一个重复的键
  17. map.set("a", "李屌屌");
  18. //通过键获得值
  19. console.log(map.get("a"));
  20. console.log(map);
  21. //forEach遍历Map
  22. map.forEach(function (value, key, map) {
  23. //三个参数分别代表
  24. //value:map当前键对应的值。
  25. //key:map当前的键
  26. //map:就是map本身的对象
  27. console.log("键:" + key);
  28. console.log("值:" + value);
  29. if (key == 'd') {
  30. console.log(value.age);
  31. }
  32. });
  33. var arr = ["a", "b", "c"];
  34. //数组遍历
  35. arr.forEach(function (element, idx, array) {
  36. //element:数组的当前元素
  37. //idx:下标
  38. //array:当前array对象
  39. console.log(array);
  40. });
  41. </script>
  42. </body>
  43. </html>

Map之公司部门员工的业务

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>公司部门员工的业务编程</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. /*
  10. 3、业务编程题。
  11. 需求:
  12. 员工类(Staff)
  13. 属性:
  14. 员工姓名(name)
  15. 所在部门名字(departmentName)(是一个字符串)
  16. 入职时间(time)(例如:2015 这种格式的字符串即可)
  17. 公司类(Company)
  18. 属性:
  19. 部门:(departmentMap) (类型是一个Map,Map的键是部门的名字,值是对应部门的员工数组,员工数组
  20. 里是每个员工对象)
  21. 方法:
  22. 1、添加员工
  23. 要求:首先员工姓名和入职时间不能同时重复。 如果添加的员工所在部门不存在,你需要帮他
  24. 创建该部门,如果存在直接加入该部门即可。
  25. 2、交换员工的部门(参数是:部门员工和要交换的部门名字)
  26. */
  27. //员工构造函数
  28. var Staff = function (name, time, departmentName) {
  29. this.name = name;
  30. this.time = time;
  31. this.departmentName = departmentName;
  32. }
  33. //公司构造函数
  34. var Company = function () {
  35. this.departmentMap = new Map();
  36. }
  37. //添加员工的方法
  38. // 要求:首先员工姓名和入职时间不能同时重复。 如果添加的员工所在部门不存在,你需要帮他
  39. // 创建该部门,如果存在直接加入该部门即可。
  40. Company.prototype.addStaff = function (newStaff) {
  41. //声明一个return标识
  42. var flag = true;
  43. //遍历部门的map
  44. this.departmentMap.forEach(function (value, key, map) {
  45. //遍历每个员工数组,因为value就已经是map遍历出来的值,值就是员工数组。
  46. value.forEach(function (element, idx, array) {
  47. //判断每个员工对象是否和新员工对象的名字和入职时间相同。
  48. if (element.name == newStaff.name && element.time == newStaff.time) {
  49. console.log("不能重复添加新员工!");
  50. //当员工重复的时候,flag=false
  51. flag = false;
  52. // return;
  53. }
  54. });
  55. });
  56. //当flag为true的时候,说明新加的员工名字和时间不重复,则可以新加员工。
  57. if (flag) {
  58. //获得新添加员工所在部门,判断其部门是否存在(以后这块儿用map的has方法判断)
  59. var staffArray = this.departmentMap.get(newStaff.departmentName);
  60. if (typeof staffArray == 'undefined') {
  61. //新创建的员工数组
  62. staffArray = new Array();
  63. //新添加的员工
  64. staffArray.push(newStaff);
  65. //新创建的部门
  66. this.departmentMap.set(newStaff.departmentName, staffArray);
  67. } else {
  68. //如果部门存在直接添新员工即可
  69. staffArray.push(newStaff);
  70. }
  71. }
  72. }
  73. //查看所有员工的方法(格式化输出)
  74. Company.prototype.showAllStaffs = function () {
  75. //遍历部门map
  76. this.departmentMap.forEach(function (value, key, map) {
  77. //打印部门
  78. console.log(key + ":");
  79. //遍历员工数组
  80. value.forEach(function (element, idx, array) {
  81. console.log(" 姓名:" + element.name + "\n 时间:" + element.time);
  82. });
  83. });
  84. }
  85. //交换员工所在部门(参数:要交换的员工对象,要交换的部门名字)
  86. Company.prototype.changeStaffDepartment = function (changeStaff, changeDepartmentName) {
  87. //1、把该员工之前所在部门剔出去。
  88. // this.departmentMap.delete("品质保障部");
  89. //遍历部门map
  90. this.departmentMap.forEach(function (value, key, map) {
  91. //遍历员工数组
  92. value.forEach(function (element, idx, array) {
  93. //找出要交换的员工
  94. if (element === changeStaff) {
  95. //删除该员工
  96. value.splice(idx, 1);
  97. //改变下标
  98. // idx--; 注意:因为新加员工的时候,已经判断不能重复添加,所以这里只能找到唯一的一个员工对象进行删除。
  99. return;
  100. }
  101. });
  102. });
  103. //添加到新部门
  104. //判断新加部门存不存在,不存在就创建该部门,把这个人加进去。
  105. var staffArray = this.departmentMap.get(changeDepartmentName);
  106. if (typeof staffArray == 'undefined') {
  107. staffArray = new Array();
  108. staffArray.push(changeStaff);
  109. this.departmentMap.set(changeDepartmentName, staffArray);
  110. } else {
  111. staffArray.push(changeStaff);
  112. }
  113. }
  114. //创建公司对象
  115. var c = new Company();
  116. //创建很多员工对象
  117. var s1 = new Staff("小芳", "2015", "品质保障部");
  118. var s2 = new Staff("小薇", "2016", "品质保障部");
  119. var s3 = new Staff("小芳", "2015", "独立部");
  120. var s4 = new Staff("小芳", "2016", "程序猿鼓励部");
  121. var s5 = new Staff("小黑", "2013", "单身部");
  122. //添加新员工
  123. c.addStaff(s1);
  124. c.addStaff(s2);
  125. c.addStaff(s3);
  126. c.addStaff(s4);
  127. c.addStaff(s5);
  128. //显示所有员工及部门
  129. c.showAllStaffs();
  130. //打印分隔符
  131. console.log("\n ================");
  132. //交换员工部门
  133. c.changeStaffDepartment(s2, "程序猿鼓励部");
  134. c.changeStaffDepartment(s4, "UI设计部");
  135. //显示所有员工
  136. c.showAllStaffs();
  137. </script>
  138. </body>
  139. </html>

字符串题


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>字符串题</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. /*
  10. (易)
  11. 传入:"iOS Android HTML5 Java"
  12. 返回:"avaJ 5LMTH diordnA SOi"
  13. 函数名:function reverseString (str) ;
  14. //字符串也可以进行遍历,可以得到每个字符
  15. */
  16. function reversString (str) {
  17. //最终返回结果
  18. var resultStr = "";
  19. //逆序遍历
  20. for (var i = str.length - 1; i >= 0; i--) {
  21. //得到每个字符
  22. var tempChar = str[i];
  23. resultStr += tempChar;
  24. }
  25. return resultStr;
  26. }
  27. console.log(reversString("iOS Android HTML5 Java"));
  28. /*
  29. (难)
  30. 传入:"iOS Android HTML5 Java"
  31. 返回:"SOi diordnA 5LMTH avaJ"
  32. 函数名: function reverseStringByWords (str) ;
  33. */
  34. function reverseStringByWords (str) {
  35. //返回结果
  36. var resultStr = "";
  37. //切割字符串
  38. var arr = str.split(" ");
  39. //遍历数组
  40. for (var i = 0; i < arr.length; i++) {
  41. //得到每个元素(其实就是每个单词字符串)
  42. var tempStr = arr[i];
  43. //逆序遍历每个tempStr
  44. for (var j = tempStr.length - 1; j >= 0; j--) {
  45. //获得每个字符
  46. var tempChar = tempStr[j];
  47. //拼接起来
  48. resultStr += tempChar;
  49. }
  50. resultStr += " ";
  51. }
  52. //这么做的话最后一步记得截取空格。会返回一个新的字符串
  53. return resultStr.substr(0, resultStr.length - 1);
  54. }
  55. console.log(reverseStringByWords("iOS Android HTML5 Java"));
  56. </script>
  57. </body>
  58. </html>

改变按钮背景色


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>按钮改变背景颜色</title>
  6. </head>
  7. <body>
  8. <input type="button" id="btn_1" name="btn" value="开始" />
  9. <span id="span_1">0</span>
  10. <br />
  11. <input type="button" id="btn_2" name="btn" value="开始" />
  12. <span id="span_2">0</span>
  13. <br />
  14. <input type="button" id="btn_3" name="btn" value="开始" />
  15. <span id="span_3">0</span>
  16. <script type="text/javascript">
  17. /*
  18. 页面有3个按钮(每个按钮默认的value是“开始”)对应3个span(span内的innerHTML默认是0)。
  19. 要求:每点击一个按钮(value的值开始变停止),
  20. 开启一个轮询定时器进行对应span标签内的数字从0-255随机滚动,
  21. 在点击一下(value的值停止变开始,定时器也跟着停止。)要求得到3个的span内innerHTML的值,
  22. 并且通过这三个值把body的背景颜色改变一下。(也就是说每次点击某个按钮后,
  23. 先判断其他两个是否停下来,全部都停下来后才可以去改变背景颜色)
  24. */
  25. //获取所有button元素
  26. var buttons = document.getElementsByName("btn");
  27. //给每个button添加单击事件
  28. for (var i = 0; i < buttons.length; i++) {
  29. buttons[i].onclick = function () {
  30. //定时器的timerId
  31. var timerId;
  32. if (this.value == '开始') {
  33. this.value = '停止';
  34. console.log(this.id);
  35. //需要保存当前this指针,供内部函数使用
  36. var that = this;
  37. //如果是多个按钮触发同一个事件的话,定时器不要写在外面。写在外面会共享同一个定时器,会有各种问题。
  38. timerId = setInterval(function () {
  39. //在轮训的时候点击按钮停止
  40. if (that.value == '开始') {
  41. clearInterval(timerId);
  42. if ($("btn_1").value == '开始' && $("btn_2").value == '开始' && $("btn_3").value == '开始') {
  43. //改变背景颜色
  44. document.body.style.backgroundColor = "rgb(" + $('span_1').innerHTML + "," + $('span_2').innerHTML + "," + $('span_3').innerHTML + ")";
  45. };
  46. }
  47. //得到点击按钮id的编号
  48. // console.log(that.id);
  49. var idx = that.id.split("_")[1];
  50. //获得对应的span元素,并且赋值
  51. $("span_" + idx).innerHTML = Math.floor(Math.random() * 256);
  52. }, 100);
  53. // fn(true);
  54. } else {
  55. this.value = '开始';
  56. // clearInterval(timerId);
  57. }
  58. }
  59. }
  60. //封装通过ID获取元素的方法
  61. function $ (idName) {
  62. return document.getElementById(idName);
  63. }
  64. // var fn = (function () {
  65. // var timerId;
  66. // return function () {
  67. // timerId....定时器
  68. // }
  69. // })();
  70. </script>
  71. </body>
  72. </html>

ajax请求考试json


  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>作业3</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. /*
  10. * 3、综合题
  11. (难)
  12. 按步骤做:
  13. 1)通过ajax解析本地服务器的kaoshi.json文件,在解析的过程中封装数据模型(也就是提前准备好
  14. 相应的构造函数,json文件中有什么,就弄多少构造函数。最后解析的方法,会返回一个班级的
  15. 对象,也就是说,有这个班级对象,就能有所有的数据)
  16. 函数名:function analysisJSON(){};
  17. 2)给班级构造函数定义:
  18. - 显示所有学生信息的方法(先按照学生名字长度进行有大到小排序,如果长度相等,在按照
  19. 字符串大小进行有大到小排序)
  20. 方法名:showAllStudentsSortByName();
  21. 3)给班级构造函数定义:
  22. - 改变每个学生性别的方法(男变女,女变男),然后在该方法内部调用显示所有学生信息的方法
  23. 方法名:changeAllStudentsSex();
  24. *
  25. */
  26. //1、ajax异步请求json文件
  27. var xhr;
  28. if (window.XMLHttpRequest) {
  29. xhr = new XMLHttpRequest();
  30. } else if (window.ActiveXObject) {
  31. var versions = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
  32. for (var i = 0; i < versions.length; i++) {
  33. try {
  34. xhr = new ActiveXObject(versions[i]);
  35. break;
  36. } catch (e) {
  37. alert(e);
  38. }
  39. }
  40. } else {
  41. throw new Error("浏览器不支持Ajax");
  42. }
  43. //打开链接
  44. xhr.open("GET", "kaoshi.json", true);
  45. //发送请求
  46. xhr.send(null);
  47. //接收服务器响应
  48. xhr.onreadystatechange = function () {
  49. if (xhr.readyState == 4 && xhr.status == 200) {
  50. //转换成json字面量对象
  51. var jsonObj = JSON.parse(xhr.responseText);
  52. console.log(jsonObj);
  53. //获得班级的字面量对象
  54. var classObj = jsonObj.class;
  55. // 创建班级数据模型的对象
  56. var classModel = new ClassModel(classObj.className);
  57. //获得老师字面量对象
  58. var teacherObj = classObj.teacher;
  59. //创建老师数据模型的对象
  60. var teacherModel = new TeacherModel(teacherObj.sex, teacherObj.teacherName);
  61. //把老师的数据模型对象赋值给 班级数据模型对象的老师属性
  62. classModel.teacher = teacherModel;
  63. //获得字面量学生数组
  64. var studentArray = classObj.studentArray;
  65. //遍历字面量学生数组
  66. for (var tempStudentIdx in studentArray) {
  67. //得到每一个学生字面量对象
  68. var studentObj = studentArray[tempStudentIdx];
  69. //创建学生数据模型对象
  70. var studentModel = new StudentModel(studentObj.sex, studentObj.studentName, studentObj.studentCode);
  71. //添加学生数据模型到班级的学生数组中
  72. classModel.studentArray.push(studentModel);
  73. }
  74. // console.log(classModel);
  75. //显示所有学生信息
  76. classModel.showAllStudentsSortByName();
  77. console.log("\n ==================== ");
  78. //改变学生性别
  79. classModel.changeAllStudentsSex();
  80. }
  81. }
  82. //班级模型构造函数
  83. function ClassModel (className, teacher) {
  84. this.className = className;
  85. this.studentArray = new Array();
  86. this.teacher = teacher;
  87. }
  88. //显示所有学生信息的方法
  89. ClassModel.prototype.showAllStudentsSortByName = function () {
  90. //冒泡排序
  91. for (var i = 0; i < this.studentArray.length - 1; i++) {
  92. for (var j = 0; j < this.studentArray.length - i - 1; j++) {
  93. //获得要交换位置的两个元素
  94. var tempStudent1 = this.studentArray[j];
  95. var tempStudent2 = this.studentArray[j + 1];
  96. //判断名字的长度
  97. if (tempStudent1.studentName.length < tempStudent2.studentName.length) {
  98. //交换位置
  99. this.studentArray.splice(j, 1, tempStudent2);
  100. this.studentArray.splice(j + 1, 1, tempStudent1);
  101. } else if (tempStudent1.studentName.length == tempStudent2.studentName.length) {
  102. //如果长度相同,在比较大小
  103. if (tempStudent1.studentName <= tempStudent2.studentName) {
  104. //交换位置
  105. this.studentArray.splice(j, 1, tempStudent2);
  106. this.studentArray.splice(j + 1, 1, tempStudent1);
  107. }
  108. }
  109. }
  110. }
  111. //显示学生信息
  112. //遍历学生数组
  113. this.studentArray.forEach(function (element) {
  114. console.log("姓名:" + element.studentName + " 性别:" + element.sex + " 学号:" + element.studentCode);
  115. });
  116. }
  117. //改变学生性别的方法
  118. ClassModel.prototype.changeAllStudentsSex = function () {
  119. //遍历所有学生
  120. this.studentArray.forEach(function (element) {
  121. if (element.sex == '男') {
  122. element.sex = '女';
  123. } else {
  124. element.sex = '男';
  125. }
  126. });
  127. //调用显示学生信息的方法
  128. this.showAllStudentsSortByName();
  129. }
  130. //老师模型构造函数
  131. function TeacherModel (sex, teacherName) {
  132. this.sex = sex;
  133. this.teacherName = teacherName;
  134. }
  135. //学生的构造函数
  136. function StudentModel (sex, studentName, studentCode) {
  137. this.sex = sex;
  138. this.studentName = studentName;
  139. this.studentCode = studentCode;
  140. }
  141. </script>
  142. </body>
  143. </html>
  1. {
  2. "class" : {
  3. "className" : "HTML51604",
  4. "studentArray" : [
  5. {
  6. "sex" : "男",
  7. "studentCode" : "160401",
  8. "studentName" : "丁环宇"
  9. },
  10. {
  11. "sex" : "男",
  12. "studentCode" : "160402",
  13. "studentName" : "武戈"
  14. },
  15. {
  16. "sex" : "男",
  17. "studentCode" : "160403",
  18. "studentName" : "严帅"
  19. },
  20. {
  21. "sex" : "女",
  22. "studentCode" : "160404",
  23. "studentName" : "马丹丹"
  24. },
  25. {
  26. "sex" : "女",
  27. "studentCode" : "160405",
  28. "studentName" : "卢欢"
  29. },
  30. {
  31. "sex" : "男",
  32. "studentCode" : "160406",
  33. "studentName" : "段俊岩"
  34. },
  35. {
  36. "sex" : "男",
  37. "studentCode" : "160407",
  38. "studentName" : "吴贤青"
  39. },
  40. {
  41. "sex" : "女",
  42. "studentCode" : "160408",
  43. "studentName" : "马钰琦"
  44. },
  45. {
  46. "sex" : "男",
  47. "studentCode" : "160409",
  48. "studentName" : "崔晋龙"
  49. },
  50. {
  51. "sex" : "男",
  52. "studentCode" : "160410",
  53. "studentName" : "彭铖"
  54. },
  55. {
  56. "sex" : "女",
  57. "studentCode" : "160411",
  58. "studentName" : "王鹏"
  59. }
  60. ],
  61. "teacher" : {
  62. "sex" : "男",
  63. "teacherName" : "刘升旭"
  64. }
  65. }
  66. }

jQuery梳理


jQuery知识点

  1. 【jQuery选择器】
  2. 1、重新认识下DOM
  3. jQuery的强大之处,就在于它能够简化DOM的操作。
  4. DOM(Document Object Model)文档对象模型,它就是javascript与HTML间的接口。
  5. DOM的结构就像我们生活中家族谱一样,所以我们会像家族谱那样形容DOM的结构,比如:父元素
  6. 、子元素、兄弟元素等。
  7. 2、基本选择器
  8. jQuery构造函数,通过传入CSS选择器作为参数,能让我们轻松的获得想要的元素或元素集合。
  9. - ID选择器:$("#my_div") 获得id为my_div的元素
  10. - 元素选择器:$("p") 获得所有p元素
  11. - 类选择器:$(".my_class") 获得类名为my_class的所有元素
  12. - 属性选择器:$("[name = my_name]") 获得元素name属性为 my_name的所有元素
  13. - 组合选择器:$("div.my_class") 获得类名为my_class的所有div元素
  14. 【多项选择器】
  15. 可以获得多个元素。$('div,p') 获得所有的div 和 p 元素
  16. 【层级选择器】
  17. 因为DOM的结构是有层级的,所以有的时候基本选择器满足不了我们,我们会经常使用层级选择器。
  18. 如果两个DOM元素具有层级关系,就可以使用层级选择器。
  19. 层级说的就是祖先和后代的意思。
  20. 层级之间用空格隔开
  21. $('ul.my_class li') 获取类名为my_class的ul元素下的 所有 li元素
  22. ---看层级选择器.html
  23. 【子选择器】
  24. 子选择器类似于层级选择器。
  25. $('parentNode>childNode')
  26. 不同于层级选择器的地方是,限定了层级关系一定是父子的关系才行。也就是说childNode是parentNode的直接
  27. 子节点。
  28. ---看层级选择器.html
  29. 【过滤选择器】
  30. 过滤选择器一般不单独使用,通常附加在其他选择器上使用。能够更精确找到我们想要的元素。
  31. ---看层级选择器.html
  32. 【表单选择器】
  33. 针对表单,jQuery提供了一套关于查找表单的选择器
  34. ---看表单选择器.html
  35. 【查找和过滤】
  36. 有的时候我们通过各种选择器获得相应的元素后,我们要对这些结果集在进一步进行过滤或者查找。
  37. ---看查找和过滤.html
  1. 【jQueryDOM节点相关】
  2. 注意:以下说的都是方法。
  3. 1、父子节点相关
  4. append:将参数中的元素插入当前元素的尾部
  5. appendTo:将当前元素插入参数元素中的尾部
  6. prepend:将参数中的元素变为当前元素的第一个子元素
  7. prependTo:将当前元素变为参数元素的第一个子元素
  8. 2、兄弟节点相关
  9. after:将参数中的元素插入到当前元素的后面
  10. insertAfter:将当前元素插入到参数元素的后面
  11. before:将参数中的元素插入到当前元素的前面
  12. insertBefore:将当前元素插入到参数元素的前面
  13. 3、移除相关
  14. remove:移除元素
  15. replaceWith:将当前元素替换成参数中的元素
  1. 【jQuery动画】
  2. 注意:以下说的都是方法
  3. 1、一些动画效果的简单写法
  4. show:显示当前元素
  5. hide:隐藏当前元素
  6. toggle:隐藏或显示当前元素
  7. fadeOut:渐变(透明度)消失当前元素
  8. fadeToggle:渐变或渐隐
  9. slideDown:从上向下出现当前元素(滑动)
  10. slideUp:从下向上隐藏当前元素(滑动)
  11. slideToggle:切换上面的效果
  12. 2、animate方法
  13. 以上那些简单的写法,实际上内部都调用了这个animate方法。
  14. animate方法可以接受3个参数:
  15. - 第一个参数设置CSS属性
  16. - 第二个参数动画时长
  17. - 第三个参数回调函数
  18. 3、stop方法、delay方法
  19. stop:表示立即停止动画
  20. delay:接受一个参数,表示暂停多少毫秒之后继续执行
  21. 【each方法遍历】
  22. 语法:$(selector).each(function (idx, element) {});
  23. idx:每个选择器的下标
  24. element:当前元素(也可以用$(this)表示)
  1. 【jQuery的事件】
  2. jQuery的事件和大部分JS事件用法相同。
  3. 例如:$(selector).click(function () {});
  4. 这里就不一一举例了。
  5. 但要说一下hover事件。
  6. hover:这个方法接受2个参数,分别代表移入和移除。也可以是一个参数,表示移入或移除触发同一个函数。
  7. 【on方法】
  8. on方法是jQuery事件的统一接口。其他事件绑定的方法其实都是on方法的实现。
  9. on方法可以接受2个参数,一个表示事件类型,一个表示回调函数。
  10. on方法也可以一次性添加2个事件,这2个事件,会触发同一个回调函数。
  11. 【off方法】
  12. off方法移除事件的回调函数
  13. 【事件的命名空间】
  14. 同一个元素有的时候可以绑定多个事件,如果想移除其中一个,可以采用“命名空间”的方式。
  15. 即为每个事件定义一个二级的名称,然后利用off方法可以移除这个二级事件名称。
  16. 【event对象】
  17. 当回调函数触发后,可以接受一个参数,这个参数就是event对象
  18. evnet的属性:
  19. - type:事件的类型
  20. - target:事件源
  21. - data:传入事件对象的数据
  22. - pageX:事件发生时,鼠标水平坐标
  23. - pageY:事件发生时,鼠标垂直坐标
  24. 【one方法(一次性事件)】
  25. one方法绑定的事件,只会调用一次这个回调函数,一般表单提交的时候会用到这个。
  26. 【解决jQuery库与其他库冲突的办法】
  27. 有的时候其他库,也会使用美元符号$。难免会造成冲突。
  28. 可以使用$.noConflict()方法来使jquery中$失效。
  29. 今天要做的东西:
  30. 1、滑动菜单
  31. 2、表格过滤查找
  32. 3、下拉框左右选人(ajax请求数据,jQuery版本)
  33. 4、正常下拉框(ajax请求数据,jQuery版本)
  34. 5、可编辑的表格(ajax请求数据,jQuery版本)
  35. 6、手风琴效果(ajax请求数据,jQuery版本)
  1. //jQuery 的 ajax通用方法
  2. $.ajax({
  3. url : "car.json", //请求路径
  4. dataType : "json", //返回数据格式
  5. async : true, //是否是异步请求,默认是异步的
  6. data : {"id" : "123"}, //参数
  7. type : "GET", //请求方式
  8. beforeSend : function () { //请求之前调用的函数
  9. console.log("==========before==============");
  10. },
  11. success : function (dd) { //成功回调函数
  12. // console.log(JSON.parse(dd));
  13. //注意:这里的格式已经帮你转换好了JS对象
  14. console.log(dd);
  15. },
  16. complete : function () { //请求完毕回调函数(无论成功失败都调用)
  17. console.log("=========complete==============");
  18. },
  19. error : function () { //失败回调函数
  20. console.log("===========error================");
  21. }
  22. });
  23. //ajax请求数据
  24. $.getJSON(' url ', null, function (data) {
  25. // data即为根数据, 已经自动反序列化为字面量对象
  26. }

jQuery 基本选择器demo

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>jQuery的基本选择器</title>
  6. <!-- 导入jQuery -->
  7. <script src="../jquery/jquery-2.2.3.js"></script>
  8. </head>
  9. <body>
  10. <p>第一个p标签。</p>
  11. <p id="my_p">id为my_p的p标签。</p>
  12. <span class="my_class" name="my_name">这是第一个span标签。</span>
  13. <p class="my_class">这是一个my_class的p标签。</p>
  14. <input class="your_class" type="button" name="my_name" value="按钮" />
  15. <script type="text/javascript">
  16. //获得id为my_p 的元素
  17. var p1 = $('#my_p');
  18. //JS版本
  19. // var p1 = document.getElementById('my_p');
  20. // p1.innerHTML 错!因为这是一个jQuery对象,只能调用jQuery的属性和方法。
  21. console.log(p1.html());
  22. //如果获取一个没有的id元素,不会像原生JS那样得到一个undefined或者是null的值,而是返回的[],这也避免了,去判断 一个元素是否是 undefined的操作。
  23. //jQuery对象和DOM对象的转换
  24. var p2 = $("#my_p"); //获得jQuery对象
  25. console.log(p2.html());
  26. var domP = p2.get(0); //jQuery对象转换成DOM对象
  27. console.log(domP.innerHTML);
  28. var p3 = $(domP); //DOM对象转换成jQuery对象
  29. console.log(p3.html());
  30. //元素选择器
  31. var p4 = $('p');
  32. //JS版本
  33. // var p4 = document.getElementsByTagName('p');
  34. console.log(p4.html()); //如果很多,默认是第一个。
  35. for (var i = 0; i < p4.length; i++ ) {
  36. console.log(p4[i].innerHTML);
  37. }
  38. //类选择器
  39. var class_1 = $('.my_class');
  40. //JS版本
  41. // var class_1 = document.getElementsByClassName('my_class');
  42. for (var i = 0; i < class_1.length; i++) {
  43. console.log(class_1[i].innerHTML);
  44. }
  45. //属性选择器
  46. var attr1 = $('[name = my_name]');
  47. console.log(attr1.length);
  48. //通过属性值前缀获得
  49. //获得class属性值前缀为my_的元素
  50. var attr2 = $('[class ^= my_]');
  51. console.log(attr2.length);
  52. //通过属性值后缀获得
  53. //获得class属性值后缀为_class的元素
  54. var attr3 = $('[class $= _class]');
  55. console.log(attr3.length);
  56. //组合选择器
  57. //元素和属性组合
  58. var zuHe1 = $('input[name = my_name]');
  59. //val() 和 JS的value 一样
  60. //打印这个button的value
  61. console.log(zuHe1.val());
  62. </script>
  63. </body>
  64. </html>

层级选择器

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>层级选择器</title>
  6. <script type="text/javascript" src="../jquery/jquery-2.2.3.js"></script>
  7. </head>
  8. <body>
  9. <div class="test_div">
  10. <ul class="lang">
  11. <li class="lang_javascript">JavaScript</li>
  12. <li class="lang_java">Java</li>
  13. <li class="lang_php">Php</li>
  14. <li class="lang_swift">Swift</li>
  15. </ul>
  16. </div>
  17. <div>
  18. <ul>
  19. <li class="lang_javascript">JavaScript</li>
  20. <li class="lang_java">Java</li>
  21. <li class="lang_php">Php</li>
  22. <li class="lang_swift">Swift</li>
  23. </ul>
  24. </div>
  25. <div style="display:none;">这是一个div</div>
  26. <script type="text/javascript">
  27. $(document).ready(function () {
  28. //多项选择器
  29. //获得所有的div和li标签
  30. var many1 = $('div,li');
  31. console.log(many1.length);
  32. //层级选择器
  33. //获得javascript所在的li标签
  34. var js_0 = $('div li.lang_javascript');
  35. console.log(js_0.length);
  36. var js_1 = $('ul li.lang_javascript');
  37. console.log(js_1.length);
  38. //获得javascript所在的li标签 ,要求li所在的ul标签的类名为lang
  39. var js_2 = $('ul.lang li.lang_javascript');
  40. console.log(js_2.html());
  41. //层级选择器相比较单个选择器的好处在于,它缩小了选择范围,因为首先先要确定祖先元素,之后再祖先元素内进行相应的查找。
  42. //子选择器
  43. var js_3 = $('ul.lang>li.lang_javascript');
  44. console.log(js_3.html());
  45. //错误案例(因为这个不是父子关系,是一种层级关系)
  46. var js_4 = $('div.test_div>li.lang_javascript');
  47. console.log(js_4.html());
  48. //过滤选择器
  49. //获得类名为lang的ul元素下所有li元素
  50. var filter_1 = $('ul.lang li');
  51. console.log(filter_1[2].innerHTML);
  52. //获得类名为lang的ul元素下所有li元素中的第一个li
  53. var filter_2 = $('ul.lang li:first');
  54. console.log(filter_2.html());
  55. //获得类名为lang的ul元素下所有li元素中的最后一个li
  56. var filter_3 = $('ul.lang li:last');
  57. console.log(filter_3.html());
  58. //获得类名为lang的ul元素下所有li元素中的某一个li
  59. var filter_4 = $('ul.lang li:eq(1)');
  60. console.log(filter_4.html());
  61. //获得类名为lang的ul元素下所有li元素中的偶数li
  62. var filter_5 = $('ul.lang li:even');
  63. console.log(filter_5[0].innerHTML + "---" + filter_5[1].innerHTML);
  64. //获得类名为lang的ul元素下所有li元素中的奇数li
  65. var filter_6 = $('ul.lang li:odd');
  66. console.log(filter_6[0].innerHTML + "===" + filter_6[1].innerHTML);
  67. //获得类名为lang的ul元素下所有li元素中的php之前的li元素
  68. var filter_7 = $('ul.lang li:lt(2)');
  69. console.log(filter_7[0].innerHTML + "===" + filter_7[1].innerHTML);
  70. //获得类名为lang的ul元素下所有li元素中的php之后的li元素
  71. var filter_8 = $('ul.lang li:gt(2)');
  72. console.log(filter_8.html());
  73. //获得隐藏的div标签
  74. var filter_9 = $('div:hidden');
  75. console.log(filter_9.html());
  76. //获得显示的div标签
  77. var filter_10 = $('div:visible');
  78. console.log(filter_10.length);
  79. });
  80. // $(function () {
  81. // });
  82. </script>
  83. </body>
  84. </html>

表单选择器

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>表单选择器</title>
  6. <script type="text/javascript" src="../jquery/jquery-2.2.3.js"></script>
  7. </head>
  8. <body>
  9. <form action="#" method="get">
  10. 姓名:<input type="text" value="周xixi" disabled/>
  11. <br />
  12. 密码:<input type="password" />
  13. <br />
  14. 性别:<input type="radio" name="r"/><input type="radio" name="r" />
  15. <br />
  16. 兴趣:<input type="checkbox" name="x" /> 跑步
  17. <input type="checkbox" name="x" /> 健身
  18. <input type="checkbox" name="x" /> 打LOL
  19. <br />
  20. 国籍:<select multiple>
  21. <option>中国</option>
  22. <option>美国</option>
  23. <option>日本</option>
  24. <option>俄罗斯</option>
  25. </select>
  26. <br />
  27. <input type="button" value="注册" onclick="btn_click();" />
  28. </form>
  29. <script type="text/javascript">
  30. function btn_click () {
  31. //表单选择器
  32. //获得所有input元素
  33. var form_1 = $(":input");
  34. console.log(form_1.length);
  35. //获得type为text的元素
  36. var form_2 = $(':text');
  37. console.log(form_2.val());
  38. //获得所有被选中的元素
  39. var form_3 = $(':checked');
  40. console.log(form_3.length);
  41. //获得所有checkbox被选中的元素
  42. var form_4 = $('[type=checkbox]:checked');
  43. var form_5 = $(':checkbox:checked');
  44. console.log(form_5.length);
  45. //获得被selected选中的元素
  46. var form_6 = $(':selected');
  47. console.log(form_6.val());
  48. console.log(form_6.length);
  49. //获得禁用的表单元素
  50. var form_7 = $(':disabled');
  51. console.log(form_7.val());
  52. }
  53. </script>
  54. </body>
  55. </html>

查找和过滤

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>查找和过滤</title>
  6. <script type="text/javascript" src="../jquery/jquery-2.2.3.js"></script>
  7. </head>
  8. <body>
  9. <div class="test_div">
  10. <ul class="lang">
  11. <li class="js">javascript</li>
  12. <li class="py">python</li>
  13. <li class="java">java</li>
  14. <li id="swift">swift</li>
  15. <li name="php">php</li>
  16. </ul>
  17. </div>
  18. <script type="text/javascript">
  19. $(document).ready(function () {
  20. /*
  21. first()、 last()、eq()方法(都会参照上一步的结果集)
  22. */
  23. //第一个
  24. var f_1 = $('ul.lang li').first();
  25. console.log(f_1.html());
  26. //最后一个
  27. var f_2 = $('ul.lang li').last();
  28. console.log(f_2.html());
  29. //某一个
  30. var f_3 = $('ul.lang li').eq(3);
  31. console.log(f_3.html());
  32. /*
  33. next()、prev()方法
  34. 下一个、上一个
  35. */
  36. var f_4 = $('ul.lang li').eq(3).next();
  37. console.log(f_4.html());
  38. var f_5 = $('ul.lang li').last().prev().prev();
  39. console.log(f_5.html());
  40. /*
  41. parent()、parents()、children();
  42. parent():某元素的父元素
  43. parents():某元素的祖先元素们(直到html)
  44. children():某元素的孩子元素们
  45. */
  46. var f_6 = $('ul.lang li').first().parent();
  47. console.log(f_6.html());
  48. var f_7 = $('ul.lang li').first().parents();
  49. console.log(f_7.length);
  50. //parents() 可以设置参数,进行限制
  51. var f_8 = $('ul.lang li').first().parents('div');
  52. console.log(f_8.html());
  53. //children()方法是获得某个节点下的所有孩子节点
  54. var f_9 = $('ul.lang').children();
  55. console.log(f_9.length);
  56. //获得ul下的name为php的孩子们
  57. var f_10 = $('ul.lang').children('[name=php]');
  58. console.log(f_10.html());
  59. /*
  60. siblings()、nextAll()、preAll() 都是同一级操作
  61. siblings():获得当前元素的所有兄弟元素
  62. nextAll():获得当前元素的所有后面的兄弟元素
  63. prevAll():获得当前元素的所有前面的兄弟元素
  64. */
  65. var f_11 = $('ul.lang li').first().siblings().eq(2);
  66. console.log(f_11.html());
  67. var f_12 = $('ul.lang li').first().siblings(':eq(2)');
  68. console.log(f_12.html());
  69. var f_13 = $('ul.lang li').eq(2).nextAll(':eq(1)');
  70. // console.log(f_13.length);
  71. console.log(f_13.html());
  72. var f_14 = $('ul.lang li').last().prevAll(':eq(0)');
  73. console.log(f_14.html());
  74. /*
  75. find()
  76. 查找某个元素的所有子元素(按照条件)
  77. filter()
  78. 得到某些结果集后进行条件过滤
  79. not()
  80. 得到某些结果集后,利用not()方进行进行排除
  81. */
  82. var f_15 = $('ul.lang').find('.java');
  83. console.log(f_15.html());
  84. var f_16 = $('ul.lang li').filter('.java');
  85. console.log(f_16.html());
  86. var f_17 = $('ul.lang li').not('[name=php]').last();
  87. // console.log(f_17.length);
  88. console.log(f_17.html());
  89. });
  90. </script>
  91. </body>
  92. </html>

jQuery对象的方法

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>DOM的一些简单方法使用</title>
  6. <script type="text/javascript" src="../jquery/jquery-2.2.3.js"></script>
  7. <style type="text/css">
  8. .my_class {
  9. font-size: 30px;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div>
  15. <ul id="lang">
  16. <li class="js">javascript</li>
  17. <li name="js_lang">javascript &amp; java</li>
  18. </ul>
  19. </div>
  20. <p id="my_p">这是一个段落。</p>
  21. <div id="my_div">
  22. </div>
  23. <input type="button" value="html/text" onclick="htFn(this);" />
  24. <br />
  25. <input type="button" value="addClass" onclick="addClassFn(this);" />
  26. <br />
  27. <input type="button" value="removeClass" onclick="removeClassFn(this);" />
  28. <br />
  29. <input type="button" value="toggleClass" onclick="toggleClassFn(this);" />
  30. <br />
  31. <input type="button" value="css" onclick="cssFn(this);" />
  32. <br />
  33. <input type="button" value="val" onclick="valFn(this);" />
  34. <br />
  35. <input type="button" value="attr" onclick="attrFn(this);" />
  36. <br />
  37. <input type="button" value="removeAttr" onclick="removeAttrFn(this);" />
  38. <script type="text/javascript">
  39. //html() 和 text()
  40. //获取和设置
  41. function htFn (ele) {
  42. //获取
  43. var ht = $('#lang li[name=js_lang]').html();
  44. var txt = $('#lang li[name=js_lang]').text();
  45. console.log(ht);
  46. console.log(txt);
  47. //设值
  48. $('#my_div').html('<span style="color:red;">这其实是一个span。</span>');
  49. $('#my_div span').text("JS & Java");
  50. }
  51. //添加类class
  52. function addClassFn (ele) {
  53. $('#my_p').addClass('my_class');
  54. }
  55. //移除类class
  56. function removeClassFn (ele) {
  57. $('#my_p').removeClass('my_class');
  58. }
  59. //自动切换添加和移除类的方法
  60. function toggleClassFn (ele) {
  61. $('#my_p').toggleClass('my_class');
  62. }
  63. //css() 可以获取和设置 元素的css样式
  64. function cssFn (ele) {
  65. //获取某个样式属性值
  66. var cssValue = $('#my_p').css('font-size');
  67. console.log(cssValue);
  68. //设置某个样式属性值
  69. $('#my_p').css('font-size', '50px');
  70. //设置多个可以这么写
  71. $('#my_p').css({
  72. 'font-size' : '30px',
  73. 'color' : 'red'
  74. });
  75. }
  76. //val() 可以获取值 和 设置值
  77. function valFn (ele) {
  78. //获取
  79. //注意:ele是JS的this指针,需要转换成jQuery对象才能使用val()方法
  80. var valValue = $(ele).val();
  81. console.log(valValue);
  82. //设值
  83. $(ele).val('改变值了');
  84. }
  85. //attr(); 获得html元素的某个属性的值,也可以设值
  86. function attrFn (ele) {
  87. //获取
  88. var attrValue = $(ele).attr('type');
  89. console.log(attrValue);
  90. //设值
  91. $(ele).attr('value', '随便');
  92. }
  93. //removeAttr() 这个方法是移除某个属性
  94. function removeAttrFn (ele) {
  95. //移除
  96. $(ele).removeAttr('value');
  97. console.log($(ele).attr('value'));
  98. }
  99. </script>
  100. </body>
  101. </html>

DOM节点的相关

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>DOM节点相关</title>
  6. <script type="text/javascript" src="../jquery/jquery-2.2.3.js"></script>
  7. <style type="text/css">
  8. .my_class1{
  9. background: red;
  10. width: 300px;
  11. height: auto;
  12. }
  13. .my_class2{
  14. background: green;
  15. width: 300px;
  16. height: 200px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="my_class1">
  22. </div>
  23. <div class="my_class2">
  24. <p>我是第一个p标签</p>
  25. <p>我是第二个p标签</p>
  26. </div>
  27. <input type="button" id="append" value="append" />
  28. <br />
  29. <input type="button" id="appendTo" value="appendTo" />
  30. <br />
  31. <input type="button" id="prepend" value="prepend" />
  32. <br />
  33. <input type="button" id="prependTo" value="prependTo" />
  34. <br />
  35. <input type="button" id="after" value="after" />
  36. <br />
  37. <input type="button" id="insertAfter" value="insertAfter" />
  38. <br />
  39. <input type="button" id="before" value="before" />
  40. <br />
  41. <input type="button" id="insertBefore" value="insertBefore" />
  42. <br />
  43. <input type="button" id="remove" value="remove" />
  44. <br />
  45. <input type="button" id="replaceWith" value="replaceWith" />
  46. <br />
  47. <script type="text/javascript">
  48. $(function () {
  49. //append:将参数中的元素插入当前元素的尾部
  50. $('#append').get(0).onclick = function () {
  51. $('.my_class1').append('<p>append的段落标签</p>');
  52. }
  53. //appendTo:将当前元素插入参数元素中的尾部
  54. $('#appendTo').get(0).onclick = function () {
  55. $('<p>appendTo的段落标签</p>').appendTo($('.my_class1'));
  56. }
  57. //prepend:将参数中的元素变为当前元素的第一个子元素
  58. $('#prepend').get(0).onclick = function () {
  59. $('.my_class2').prepend($('.my_class2 p:last'));
  60. }
  61. //prependTo:将当前元素变为参数元素的第一个子元素
  62. $('#prependTo').get(0).onclick = function () {
  63. $('.my_class2 p:last').prependTo($('.my_class2'));
  64. }
  65. //after:将参数中的元素插入到当前元素的后面
  66. $('#after').get(0).onclick = function () {
  67. $('.my_class2 p:last').after($('.my_class2 p:first'));
  68. }
  69. //insertAfter:将当前元素插入到参数元素的后面
  70. $('#insertAfter').get(0).onclick = function () {
  71. $('.my_class2 p:first').insertAfter($('.my_class2 p:last'));
  72. }
  73. //before:将参数中的元素插入到当前元素的前面
  74. $("#before").get(0).onclick = function () {
  75. $('.my_class2 p:first').before($('.my_class2 p:last'));
  76. }
  77. //insertBefore:将当前元素插入到参数元素的前面
  78. $('#insertBefore').get(0).onclick = function () {
  79. $('.my_class2 p:last').insertBefore($('.my_class2 p:first'));
  80. }
  81. //remove:移除元素
  82. $('#remove').get(0).onclick = function () {
  83. $(this).remove();
  84. }
  85. //replaceWith:将当前元素替换成参数中的元素
  86. $('#replaceWith').get(0).onclick = function () {
  87. $(this).replaceWith('<p style="color:red;">被我替换了!!</p>');
  88. }
  89. });
  90. </script>
  91. </body>
  92. </html>

jQuery 动画

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>jQuery动画</title>
  6. <script type="text/javascript" src="../jquery/jquery-2.2.3.js"></script>
  7. <style type="text/css">
  8. .my_class{
  9. background: red;
  10. width: 300px;
  11. height: 300px;
  12. position: absolute;
  13. left: 300px;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div class='my_class'>
  19. </div>
  20. <input type="button" value="show" />
  21. <br />
  22. <input type="button" value="hide" />
  23. <br />
  24. <input type="button" value="toggle" />
  25. <br />
  26. <input type="button" value="fadeIn" />
  27. <br />
  28. <input type="button" value="fadeOut" />
  29. <br />
  30. <input type="button" value="fadeToggle" />
  31. <br />
  32. <input type="button" value="slideDown" />
  33. <br />
  34. <input type="button" value="slideUp" />
  35. <br />
  36. <input type="button" value="slideToggle" />
  37. <br />
  38. <input type="button" value="animate" />
  39. <br />
  40. <input type="button" value="stop" />
  41. <br />
  42. <input type="button" value="delay" />
  43. <br />
  44. <script type="text/javascript">
  45. $(function () {
  46. //show
  47. $('input[value=show]').click(function () {
  48. // $('.my_class').show();
  49. // $('.my_class').show(3000);
  50. /*
  51. 关于速度的关键字(字符串)
  52. fast: 快
  53. normal: 正常
  54. slow:慢
  55. 可以通过以下方式修改内置的速度常量值
  56. jQuery.fx.speeds.fast = 毫秒值
  57. jQuery.fx.speeds.normal = 毫秒值
  58. jQuery.fx.speeds.slow = 毫秒值
  59. 修改的时候注意作用域问题
  60. 还可以通过这个方式自定义速度关键字
  61. jQuery.fx.speeds.myFast = 毫秒值
  62. */
  63. // $('.my_class').show('slow');
  64. //还可以接受额外的参数,这个参数是回调函数
  65. $('.my_class').show('slow', function () {
  66. alert('动画执行完毕!');
  67. });
  68. });
  69. //hide
  70. $('input[value=hide]').click(function () {
  71. // $('.my_class').hide();
  72. // $('.my_class').hide(3000);
  73. jQuery.fx.speeds.fast = 5000;
  74. $('.my_class').hide('fast');
  75. });
  76. //toggle
  77. $('input[value=toggle]').click(function () {
  78. $('.my_class').toggle(2000);
  79. });
  80. //fadeIn
  81. $('input[value=fadeIn]').click(function () {
  82. $('.my_class').fadeIn(3000);
  83. });
  84. //fadeOut
  85. $('input[value=fadeOut]').click(function () {
  86. $('.my_class').fadeOut(3000);
  87. });
  88. //fadeToggle
  89. $('input[value=fadeToggle]').click(function () {
  90. $('.my_class').fadeToggle(3000);
  91. });
  92. //slideDown
  93. $('input[value=slideDown]').click(function () {
  94. $('.my_class').slideDown(3000);
  95. });
  96. //slideUp
  97. $('input[value=slideUp]').click(function () {
  98. $('.my_class').slideUp(3000);
  99. });
  100. //slideToggle
  101. $('input[value=slideToggle]').click(function () {
  102. $('.my_class').slideToggle(3000);
  103. });
  104. //animate
  105. $('input[value=animate]').click(function () {
  106. $('.my_class').animate({
  107. // left : "500px",
  108. left : "+=200",
  109. opacity : 0.25
  110. // background : 'blue' 颜色设置不了
  111. }, 5000, function () {
  112. alert("执行完毕");
  113. });
  114. });
  115. //stop
  116. $('input[value=stop]').click(function () {
  117. $('.my_class').stop();
  118. });
  119. //delay 接受一个参数,表示暂停多少毫秒之后继续执行
  120. $('input[value=delay]').click(function () {
  121. $('.my_class').fadeOut(2000).delay(2000).slideDown(2000);
  122. });
  123. });
  124. </script>
  125. </body>
  126. </html>

显示隐藏按钮作业


  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>作业</title>
  6. <style type="text/css">
  7. *{ margin:0; padding:0;}
  8. body {font-size:12px;text-align:center;}
  9. a { color:#04D; text-decoration:none;}
  10. a:hover { color:#F50; text-decoration:underline;}
  11. .SubCategoryBox {width:600px; margin:0 auto; text-align:center;margin-top:40px;}
  12. .SubCategoryBox ul { list-style:none;}
  13. .SubCategoryBox ul li { display:block; float:left; width:200px; line-height:20px;}
  14. .showmore { clear:both; text-align:center;padding-top:10px;}
  15. .showmore a { display:block; width:120px; margin:0 auto; line-height:24px; border:1px solid #AAA;}
  16. .showmore a span { padding-left:15px; background:url(img/down.gif) no-repeat 0 0;}
  17. .promoted a { color:#F50;}
  18. </style>
  19. <!-- 引入jQuery -->
  20. <script src="../jquery/jquery-2.2.3.js"></script>
  21. </head>
  22. <body>
  23. <div class="SubCategoryBox">
  24. <ul>
  25. <li ><a href="#">佳能</a><i>(30440) </i></li>
  26. <li ><a href="#">索尼</a><i>(27220) </i></li>
  27. <li ><a href="#">三星</a><i>(20808) </i></li>
  28. <li ><a href="#">尼康</a><i>(17821) </i></li>
  29. <li ><a href="#">松下</a><i>(12289) </i></li>
  30. <li ><a href="#">卡西欧</a><i>(8242) </i></li>
  31. <li ><a href="#">富士</a><i>(14894) </i></li>
  32. <li ><a href="#">柯达</a><i>(9520) </i></li>
  33. <li ><a href="#">宾得</a><i>(2195) </i></li>
  34. <li ><a href="#">理光</a><i>(4114) </i></li>
  35. <li ><a href="#">奥林巴斯</a><i>(12205) </i></li>
  36. <li ><a href="#">明基</a><i>(1466) </i></li>
  37. <li ><a href="#">爱国者</a><i>(3091) </i></li>
  38. <li ><a href="#">其它品牌相机</a><i>(7275) </i></li>
  39. </ul>
  40. <div class="showmore">
  41. <a href="#"><span>显示全部品牌</span></a>
  42. </div>
  43. </div>
  44. <script type="text/javascript">
  45. $(function () {
  46. //1.获得要隐藏或显示的li
  47. var hideLi = $('.SubCategoryBox ul li:gt(5):not(:last)');
  48. //初始化隐藏li
  49. hideLi.css('display', 'none');
  50. //2、显示全部div的单机事件
  51. $('.showmore > a').get(0).onclick = function () {
  52. if ($('.showmore > a > span').text() == '显示全部品牌') {
  53. $('.showmore > a > span').text('隐藏部分品牌');
  54. //剩余的li显示出来
  55. hideLi.css('display', 'block');
  56. //改变图标
  57. $('.showmore > a > span').css('background', 'url(img/up.gif) no-repeat 0 0');
  58. //找到要变颜色的li
  59. // $('.SubCategoryBox ul li').filter(':eq(0),:eq(3),:eq(10)').addClass('promoted');
  60. //:contains("佳能") 查找包含佳能内容的元素
  61. $('.SubCategoryBox ul li').filter(':contains("佳能"),:contains("尼康"),:contains("奥林巴斯")').addClass('promoted');
  62. } else {
  63. $('.showmore > a > span').text('显示全部品牌');
  64. //剩余的li隐藏
  65. hideLi.css('display', 'none');
  66. //改变图标
  67. $('.showmore > a > span').css('background', 'url(img/down.gif) no-repeat 0 0');
  68. //找到要变色的li
  69. // $('.SubCategoryBox ul li').filter(':eq(0),:eq(3),:eq(10)').removeClass('promoted');
  70. $('.SubCategoryBox ul li').filter(':contains("佳能"),:contains("尼康"),:contains("奥林巴斯")').removeClass('promoted');
  71. }
  72. }
  73. });
  74. </script>
  75. </body>
  76. </html>

jQuery联动下拉菜单


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>联动下拉菜单</title>
  6. <style type="text/css">
  7. </style>
  8. <script type="text/javascript" src="../jquery-2.2.3.min.js"></script>
  9. <script type="text/javascript">
  10. $(function () {
  11. // 原生的JSON, data返回了是json格式, 需要反序列化为字面量对象: JSON.parse(data);
  12. // jQuery的getJSON()和ajax()方法返回的data为字面量对象
  13. $.getJSON('../jQuery专用.json', null, function (data) {
  14. // jQuery的each方法: each(idx,ele);
  15. // js原生的forEach方法: forEach(ele, idx, array/object);
  16. // ele: 数组中的每个省对象
  17. data.personInfos.forEach(function (ele, idx) {
  18. // 添加到left
  19. $('#left_sel').append('<option>' + ele.address +'</option>');
  20. // 添加到right
  21. // 未选择时显示所有人
  22. // 遍历每个省的人
  23. ele.persons.forEach(function (person) {
  24. // 添加name用filter来过滤
  25. $('#right_sel').append('<option name="'+ ele.address +'">' + person.name +'</option>');
  26. });
  27. // 用change事件, change事件应该加在select上,
  28. // 不能加在option上, 也不要在option上用click
  29. $('#left_sel').change(function(event) {
  30. var that = $(this);
  31. // 除了请选择以外, 全部清空
  32. $('#right_sel option').not(':first').remove();
  33. // 匹配省份动态显示人
  34. data.personInfos.forEach(function (ele, idx) {
  35. // 判断
  36. if (ele.address == that.val()) {
  37. ele.persons.forEach(function (person) {
  38. $('#right_sel').append('<option>'+ person.name +'</option>');
  39. });
  40. }
  41. });
  42. });
  43. });
  44. });
  45. }); // onload结束
  46. </script>
  47. </head>
  48. <body>
  49. <select name="" id="left_sel">
  50. <option value="">请选择地区</option>
  51. </select>
  52. <select name="" id="right_sel">
  53. <option value="">请选择人</option>
  54. </select>
  55. </body>
  56. </html>

jQuery的手风琴的效果


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>老师的手风琴的效果</title>
  6. <style type="text/css">
  7. .menu_list{
  8. width: 150px;
  9. }
  10. .menu_head{
  11. height: 30px;
  12. line-height: 30px;
  13. cursor: pointer;
  14. background: orange;
  15. text-align: center;
  16. font-size: 20px;
  17. margin:10px 0 0;
  18. border-bottom: 3px solid orangered;
  19. border-radius: 5px 5px 0 0;
  20. }
  21. .menu_body{
  22. cursor: pointer;
  23. background: lightgreen;
  24. text-align: center;
  25. }
  26. .menu_body div{
  27. border-bottom: 2px solid red;
  28. }
  29. </style>
  30. <script type="text/javascript" src="../jquery-2.2.3.min.js"></script>
  31. </head>
  32. <body>
  33. <div class="menu_list">
  34. <!-- <div class="menu_head"></div>
  35. <div class="menu_body">
  36. <div>内容1</div>
  37. <div>内容2</div>
  38. <div>内容3</div>
  39. </div> -->
  40. </div>
  41. <!-- <div class="menu_list">
  42. <div class="menu_head"></div>
  43. <div class="menu_body">
  44. <div>内容1</div>
  45. <div>内容2</div>
  46. <div>内容3</div>
  47. </div>
  48. </div>
  49. <div class="menu_list">
  50. <div class="menu_head"></div>
  51. <div class="menu_body">
  52. <div>内容1</div>
  53. <div>内容2</div>
  54. <div>内容3</div>
  55. </div>
  56. </div> -->
  57. <script type="text/javascript">
  58. $(function () {
  59. $.getJSON("../jQuery专用.json", null, function (data) {
  60. data.personInfos.forEach(function (ele, idx) {
  61. $('.menu_list').append('<div class="menu_head">'+ele.address+'</div><div class="menu_body"></div');
  62. ele.persons.forEach(function (person) {
  63. $('.menu_body:last').append('<div>' + person.name + '</div>');
  64. });
  65. });
  66. // 折叠效果
  67. $('.menu_head').click(function(event) {
  68. // 点击展开或闭合,同时其他的body闭合
  69. $(this).next('div.menu_body').slideToggle().siblings('div.menu_body').slideUp();
  70. // 点击展开或闭合
  71. //$(this).next('div.menu_body').slideToggle();
  72. });
  73. });
  74. });
  75. </script>
  76. </body>
  77. </html>

迭代器以及迭代器的模拟


JS迭代器的用法

  1. /*
  2. 遍历器(Iterator)就是这样一种机制。它是一种接口,为各种不同的数据结构提供统一的访问机制。任何数据结构只要部署Iterator接口,就可以完成遍历操作(即依次处理该数据结构的所有成员)
  3. Iterator的遍历过程是这样的。
  4. (1)创建一个指针对象,指向当前数据结构的起始位置。也就是说,遍历器对象本质上,就是一个指针对象。
  5. (2)第一次调用指针对象的next方法,可以将指针指向数据结构的第一个成员。
  6. (3)第二次调用指针对象的next方法,指针就指向数据结构的第二个成员。
  7. (4)不断调用指针对象的next方法,直到它指向数据结构的结束位置。
  8. 每一次调用next方法,都会返回数据结构的当前成员的信息。具体来说,就是返回一个包含value和done两个属性的对象。其中,value属性是当前成员的值,done属性是一个布尔值,表示遍历是否结束。
  9. */
  10. // 可迭代的对象自身或原型链上,
  11. // 必须有一个Symbol.iterator属性, 通过这个属性就能获得迭代器
  12. var str = "abc";
  13. console.log(typeof str[Symbol.iterator]); // function
  14. // 调用迭代器
  15. var iterator = str[Symbol.iterator]();
  16. console.log(iterator.next()); // Object { value="a", done=false}
  17. console.log(iterator.next()); // Object { value="b", done=false}
  18. console.log(iterator.next()); // Object { value="c", done=false}
  19. console.log(iterator.next()); // Object { done=true, value=undefined} // 调用完成
  20. console.log(iterator.next()); // Object { done=true, value=undefined}
  21. var arr = ['你','们','好'];
  22. var iterator = arr[Symbol.iterator]();
  23. console.log(iterator.next().value); // 你
  24. console.log(iterator.next().value); // 们
  25. console.log(iterator.next().value); // 好
  26. console.log(iterator.next().value); // undefined
  27. console.log(iterator.next().value); // Object { done=true, value=undefined}

大神模拟迭代器

  1. // 大神的模拟
  2. function makeIterator(array) {
  3. var nextIndex = 0;
  4. return {
  5. next: function() {
  6. return nextIndex < array.length ?
  7. {value: array[nextIndex++], done: false} :
  8. {value: undefined, done: true};
  9. }
  10. };
  11. }

我自己模拟迭代器

  1. /*
  2. 思路: 用数组存储数据, 下标的状态用闭包保存, 每次调用自增
  3. 属性: value, done
  4. 方法: next()
  5. 返回值: 返回值应该是一个对象,对象中有next()方法
  6. 闭包用在next()方法里,
  7. return { value=参1, done=布尔值};
  8. */
  9. function myIterator(target) {
  10. // 传入的参数应该为数组或字符串
  11. if (! (typeof target == "string" || target instanceof Array)){
  12. console.log("TypeError : 数据类型不正确,请传入数组或字符串!!!");
  13. return ;
  14. }
  15. var idx = 0; // 会通过闭包保存在内存中
  16. var result = {};
  17. result.next = function () {
  18. var result1 = {};
  19. // 判断下标和长度的关系
  20. if (idx < target.length) {
  21. result1.value = target[idx++];
  22. result1.done = false;
  23. }else {
  24. result1.value = undefined;
  25. result1.done = true;
  26. }
  27. return result1;
  28. };
  29. return result;
  30. }
  31. console.group("判断测试");
  32. // number类型
  33. myIterator(1312414214);
  34. // Object类型
  35. var oo = {};
  36. myIterator(oo);
  37. console.groupEnd();
  38. console.group("字符串测试");
  39. var fn1 = myIterator("1314");
  40. var char1 = fn1.next();
  41. var char2 = fn1.next();
  42. var char3 = fn1.next();
  43. var char4 = fn1.next();
  44. var char5 = fn1.next();
  45. console.log(char1);
  46. console.log(char2);
  47. console.log(char3);
  48. console.log(char4);
  49. console.log(char5);
  50. console.log(char1.value);
  51. console.log(char2.value);
  52. console.log(char3.value);
  53. console.log(char4.value);
  54. console.log(char5.value);
  55. fn1 = null; // 释放闭包所占内存
  56. console.groupEnd();
  57. console.group("数组的测试");
  58. var fn2 = myIterator(["I", "MISS" , "U"]);
  59. var ele1 = fn2.next();
  60. var ele2 = fn2.next();
  61. var ele3 = fn2.next();
  62. var ele4 = fn2.next();
  63. var ele5 = fn2.next();
  64. console.log(ele1);
  65. console.log(ele2);
  66. console.log(ele3);
  67. console.log(ele4);
  68. console.log(ele5);
  69. console.log(ele1.value);
  70. console.log(ele2.value);
  71. console.log(ele3.value);
  72. console.log(ele4.value);
  73. console.log(ele5.value);
  74. fn1 = null; // 释放闭包所占内存
  75. console.groupEnd();

迭代器的进阶

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>迭代器的深入讨论</title>
  6. <script type="text/javascript">
  7. // 自定义的迭代器
  8. function idMaker(target) {
  9. var nextIndex = 0;
  10. if (!(typeof target =="string"|| target instanceof Array)){
  11. console.log("请传入数组或字符串");
  12. return;
  13. }
  14. return {
  15. next: function() {
  16. // 三目运算写法1
  17. //return nextIndex < target.length ?{value: target[nextIndex++], done: false} :{value: undefined, done: true};
  18. // 三目运算写法2
  19. return {value : target[nextIndex++],done: nextIndex < target.length ? false : true};
  20. }
  21. };
  22. }
  23. // 封装重复调用.next()的效果
  24. function idMakerCountObj(arrstr,count) {
  25. // 回调
  26. var fn = idMaker(arrstr);
  27. for (var i = 0; i < count ; i++) {
  28. console.log(fn.next());
  29. }
  30. }
  31. // 封装重复调用.next().value的效果
  32. function idMakerCountVal(arrstr,count) {
  33. var fn = idMaker(arrstr);
  34. for (var i = 0; i < count ; i++) {
  35. console.log(fn.next().value);
  36. }
  37. }
  38. // 调用
  39. idMakerCountObj(["I","MISS","U"],5);
  40. idMakerCountVal(["I","MISS","U"],5);
  41. //调用
  42. var str = "梦想决定你人生的上限, 而努力决定了你人生的下限! 人生无极限,努力吧!";
  43. idMakerCountVal(str,str.length);
  44. </script>
  45. </head>
  46. <body>
  47. </body>
  48. </html>

闭包


  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>闭包</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //函数嵌套,就是一个函数内部有另外一个函数
  10. function fn1 () {
  11. var num = 100;
  12. function subFn1 (num1) {
  13. console.log("num is " + (num + num1));
  14. console.log("我是一个嵌套函数的子函数。");
  15. }
  16. subFn1(50);
  17. }
  18. fn1();
  19. //可以把内部函数当做外部函数的返回值返回
  20. //这么做的好处是,我们可以在任何时候调用内部函数都行。
  21. //而且内部函数中引用的外部函数的变量,一直存在。
  22. function fn2 () {
  23. var num2 = 100;
  24. function subFn2 (param) {
  25. console.log("num2 is " + (num2 + param));
  26. }
  27. //subFn2是返回函数,subFn2()是返回运行结果
  28. return subFn2;
  29. }
  30. // console.log(fn2());
  31. //运行结果是fn2内部函数
  32. var resultFn2 = fn2();
  33. resultFn2(500);
  34. resultFn2(200);
  35. //自增操作 :可以证明其变量存在内存中,不会消失
  36. function fn3 () {
  37. var count = 0;
  38. // function subFn3 () {
  39. // console.log("count is " + (++count));
  40. // }
  41. // return subFn3;
  42. //一般都写匿名函数
  43. return function () {
  44. console.log("count is " + (++count));
  45. };
  46. }
  47. var resultFn3 = fn3();
  48. resultFn3();
  49. resultFn3();
  50. resultFn3();
  51. console.log("====================");
  52. //每次运行完外部函数,都会生成一个新的内部函数。
  53. var resultFn4 = fn3();
  54. resultFn4();
  55. resultFn4();
  56. console.log(resultFn3 === resultFn4);
  57. //需求:判断两个对象的任一属性值的大小
  58. var obj1 = {
  59. name : "a",
  60. age : 18
  61. };
  62. var obj2 = {
  63. name : "b",
  64. age : 15
  65. };
  66. //访问对象的属性不仅有(.)语法,还可以使用[]来访问
  67. console.log(obj1["name"]);
  68. console.log(obj1.age > obj2.age);
  69. //比较两个对象任意属性值大小
  70. //外部函数的参数是要比较的属性名字
  71. function comparePropertyFn (propertyName) {
  72. //返回内不函数,参数是要比较的两个对象
  73. return function (obj1, obj2) {
  74. //通过属性名字获得值
  75. var value1 = obj1[propertyName];
  76. var value2 = obj2[propertyName];
  77. //最后的判断
  78. if (value1 > value2) {
  79. return 1;
  80. } else if (value1 === value2) {
  81. return 0;
  82. } else {
  83. return -1;
  84. }
  85. };
  86. }
  87. //获得内部的比较函数
  88. //比较age属性值
  89. var resultCompareFn = comparePropertyFn("age");
  90. var resultValue = resultCompareFn(obj2, obj1);
  91. console.log(resultValue);
  92. //比较name属性值
  93. var resultCompareFn2 = comparePropertyFn("name");
  94. var resultValue2 = resultCompareFn2(obj2, obj1);
  95. console.log(resultValue2);
  96. </script>
  97. </body>
  98. </html>

异步执行


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>js异步执行</title>
  6. <script type="text/javascript">
  7. /*
  8. 【异步执行】
  9. javascript的执行环境是"单线程",单线程是指一次只能运行一个任务.
  10. 如果有多个任务,只能排队执行.
  11. 【单线程的好处】
  12. 实现简单
  13. 【单线程的坏处】
  14. 执行多个任务,如果前一个运行时间过长,或浏览器假死,后面的任务只能等待.
  15. 【同步任务】
  16. 一次只能执行一个任务
  17. 【异步任务】
  18. 把任务分成两部分 :
  19. --第一部分一般是请求数据(耗时操作),
  20. --第二部分一般是回调函数,
  21. 这个回调函数包括了对外部数据的处理.
  22. 第一部分执行完,不是立即调用第二部分, 而是将程序的执行权交给第二部分,
  23. 然后第一部分的数据完全返回会, 才会执行第二部分的任务
  24. **所以** 异步任务执行顺序和任务排列无关!!
  25. 【串行】 任务一个接一个执行
  26. 【并行】 任务并列执行
  27. 【串行和并行结合】
  28. 设置一个条件,每次最多能并行n个任务, 这样可以避免性能的消耗
  29. 【】
  30. 【】
  31. 【】
  32. 【】
  33. 【】
  34. */
  35. // 模拟一个异步的任务
  36. // console.log("***********before***********");
  37. // setTimeout(function () {
  38. // console.log("***********ok***************");
  39. // },10);
  40. // console.log("***********after************");
  41. // 异步任务函数
  42. function asynFn (arg, callbackFn) {
  43. console.log("+++++++++++++" + arg +"++++++++++++++++++");
  44. setTimeout(function () {
  45. callbackFn(arg + 1000); // 一个参数
  46. },0);
  47. }
  48. // 调异步任务
  49. // asynFn(234,function (data) {
  50. // console.log(data);
  51. // });
  52. // 调用5个异步的任务
  53. // 串行任务,但是时间参数怎么控制的么
  54. // 调用的是异步任务函数
  55. // asynFn(1,function (data) { // 此处data为形参
  56. // // 参1: 返回的结果
  57. // asynFn(data,function (data) {
  58. // asynFn(data,function (data) {
  59. // asynFn(data,function (data) {
  60. // asynFn(data,function (data) {
  61. // // 运行下
  62. // });
  63. // });
  64. // });
  65. // });
  66. // });
  67. /*************串行改进********************/
  68. // // 编写一个串行函数
  69. // // 串行函数的参数
  70. // var items = [1, 2, 3, 4, 5];
  71. // // 每个异步任务返回结果的数组
  72. // var results = [];
  73. // // 串行函数
  74. // function series (item) {
  75. // if (item){
  76. // // 开始调用异步任务
  77. // asynFn(item, function (result) {
  78. // // 把回调函数的返回值存储在最终结果数组中
  79. // results.push(result);
  80. // return series(items.shift());
  81. // });
  82. // } else {
  83. // //console.log(results);
  84. // finalFn(results); // 最终结果打印函数
  85. // }
  86. // }
  87. // finalFn完成函数
  88. function finalFn (x) {
  89. console.log(x);
  90. }
  91. // // 调用串行函数
  92. // // shift()弹出首个元素,并返回该元素
  93. // series(items.shift());
  94. /*************串行改进_End********************/
  95. /*************并行函数********************/
  96. // 参数数组
  97. // var items = [1, 2 ,3 ,4 ,5];
  98. // //返回的结果数组
  99. // var results = [];
  100. // //
  101. // items.forEach(function (item) {
  102. // // 调用异步任务
  103. // asynFn(item,function (result) {
  104. // // 添加到结果数组
  105. // results.push(result);
  106. // // 结果一起执行
  107. // // 数据一起返回
  108. // if (results.length == items.length) {
  109. // // 打印
  110. // finalFn (results);
  111. // }
  112. // });
  113. // });
  114. /*************并行执行_End********************/
  115. /*************串行+并行的结合********************/
  116. /*
  117. 因为:异步任务执行顺序和任务排列无关
  118. running++ 会运行在running--的后面
  119. */
  120. // 参数数组
  121. var items = [1, 2 ,3 ,4 ,5];
  122. //返回的结果数组
  123. var results = [];
  124. // 当前运行的任务数量
  125. var running = 0;
  126. // 每次最大任务数量
  127. var limit = 2;
  128. // 串行、并行函数
  129. function run () {
  130. // 循环为并行
  131. while (running < limit && items.length > 0) {
  132. // 调用异步任务
  133. asynFn(items.shift(),function (result) {
  134. // 存到结果数组中
  135. results.push(result);
  136. // 当前任务数量自减
  137. // 测试
  138. console.log("我是callbackFn");
  139. running--; // 写在了回调函数里, 任务结束了才自减
  140. if (items.length > 0) {
  141. // 调用run
  142. run(); // 此处为串行
  143. } else if(running === 0) {
  144. finalFn(results);
  145. }
  146. });
  147. // 每次循环当前数量++
  148. console.log("测试在callbackFn前面执行,running++");
  149. running++;
  150. }
  151. }
  152. // 调用
  153. run();
  154. /*************串行+并行的结合_End********************/
  155. /*
  156. ajax本身就是异步请求,
  157. 所以画UI和绑定事件都应该写在callback函数内
  158. 写外边会出现错误
  159. */
  160. </script>
  161. </head>
  162. <body>
  163. </body>
  164. </html>

模块化与RequireJS简单应用


模块化的思想

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>模块化</title>
  6. <script type="text/javascript">
  7. /*
  8. 【模块化】: 是一种思想,按分类思想分包或分类管理.(理解成文件夹)
  9. 随着网站内容丰富和复杂,简单的用<script>标签导入外部的JS文件不能适应现代开发了,
  10. 我们需要团队协作, 分离模块, 模块测试等复杂的需求
  11. 模块化是指在解决一个问题或一系列问题时, 按照一种分类的思想对功能进行分解处理.
  12. 模块化对于软件来说, 可以解耦软件的复杂性.
  13. 【耦合度】: 即为相关联性, 耦合度越低越好.
  14. 模块化是java和php等后端语言的必须得思想;
  15. 分包管理或分文件管理
  16. 【模块化设计必须有的能力】
  17. 1. 定义封装的模块.
  18. 2. 定义对新模块的依赖性.
  19. 3. 可以引入其他的模块.
  20. 【模块化开发需要遵循规范AMD或CMD等】
  21. 【模块化思想】
  22. 函数也算是模块
  23. 对象也是
  24. 只不过没按照模块化规范来写
  25. */
  26. // 一. 函数写法 函数本身算一个模块
  27. // 缺点:
  28. // 1.污染了全局变量, 模块之间无法保证命名是否冲突和覆盖
  29. // 2. 模块之间没有直接关系
  30. // 二. 对象写法 对象本身算一个模块
  31. // 优点:
  32. // 1. 解决了函数写法的缺点
  33. // 2. 所有的成员打包放在了这个对象中
  34. // 缺点:
  35. // 1. 可以对该模块内部的属性
  36. // 三.函数立即调用
  37. // 不暴露成员变量的目的 (命名空间)
  38. // 1. 闭包思想
  39. // 2. 优点: 外部无法修改内部的局部变量.
  40. // 四. 放大模式
  41. //
  42. //
  43. // 五. 无关放大模式
  44. //
  45. //
  46. //
  47. //
  48. var obj1 = {
  49. name : "王大可",
  50. m1 : function () {
  51. console.log("m1:" + this.name);
  52. },
  53. m2 : function () {
  54. console.log("m2:" + this.name);
  55. }
  56. };
  57. obj1.m1();
  58. obj1.name ="撼地神牛";
  59. obj1.m1();
  60. // 三.函数立即调用
  61. //1.不暴露成员变量的目的 (命名空间)
  62. var modal = (function () {
  63. var age =18;
  64. var m1 = function () {
  65. return "m1: " + age;
  66. };
  67. var m2 = function () {
  68. age++;
  69. return "m2: " + age;
  70. };
  71. return {
  72. //"m1" : m1(), // modal.m1 是属性
  73. // 把函数的指针作为value
  74. // key m1是属性, value m1是上面的变量保存的函数地址
  75. // key m1 实际是"m1"
  76. m1 : m1, // modal.m1() 是方法
  77. m2 : m2 // modal.m2() 是方法
  78. };
  79. })();
  80. console.log(modal.m1);
  81. console.log(modal.m1());
  82. console.log(modal.m2());
  83. console.log("这也是闭包");
  84. console.log(modal.m1());
  85. console.log("++++++以下用了闭包+++++++");
  86. var fn = modal.m2;
  87. console.log(fn());
  88. console.log(fn());
  89. console.log(fn());
  90. console.log(fn());
  91. console.log(fn());
  92. // 四. 放大模式
  93. // 模块化的思想给对象添加了一个方法
  94. var modal4 = {};
  95. modal4 = (function (mod) {
  96. mod.m1 = function () {
  97. console.log("被放大了, 额外添加的方法");
  98. };
  99. return mod;
  100. })(window.modal4); // 传实际的参数,写modal4也可以
  101. modal4.m1();
  102. // 五. 无关放大模式
  103. var modal5 = {};
  104. modal5 = (function (mod) {
  105. mod.m1 = function () {
  106. console.log("被放大了, 额外添加的方法");
  107. };
  108. return mod;
  109. })(window.modal5 || {});
  110. modal5.m1();
  111. </script>
  112. </head>
  113. <body>
  114. </body>
  115. </html>

RequireJS简单应用

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>RequireJS</title>
  6. <script type="text/javascript">
  7. /*
  8. 【RequireJS概念】
  9. 1. RequireJS是一个前端模块化的管理工具库.
  10. 【RequireJS基本思想】
  11. 通过一个函数将所需的模块实现装载进来. 然后返回一个新的函数 (模块).
  12. 所有的业务逻辑代码, 都会放在这个函数内部操作
  13. 【如何使用RequireJS】
  14. 去官网下载, 然后通过<script> 中src属性引用
  15. 【RequireJS相应的API】
  16. 见demo
  17. 【】
  18. 【】
  19. */
  20. </script>
  21. <!-- <script type="text/javascript" src="js/sayHello.js"></script> -->
  22. <script type="text/javascript" src="require.js"></script>
  23. <script type="text/javascript">
  24. // requireJS的基本API
  25. /*
  26. require有三个变量: define, require, requirejs
  27. require和requirejs是一个意思, 我们一般使用require
  28. define : 定义模块
  29. require : 加载模块的, 并且可以执行加载模块后的回调函数
  30. 符合AMD标准: 异步加载模块
  31. */
  32. // 利用require来加载模块 (异步加载)
  33. // 数组用来加载多个模块
  34. //require(['js/SayHello']);
  35. // 有回调函数
  36. // require(['js/SayHello'],function () {
  37. // alert("加载完成!");
  38. // })
  39. // 加载配置文件
  40. // 上面例子是加载本地文件
  41. // 也可以加载网络文件或服务器文件, 这样就不能用上述方式加载
  42. // 需要用require.config来配置一下 (没有远端环境也可以自己配置, 强大!!!)
  43. // 省略.js
  44. // 百度的jQuery库: http://libs.baidu.com/jquery/1.9.0/jquery.min.js
  45. // require.config({
  46. // paths : {
  47. // jquery : ["http://libs.baidu.com/jquery/1.9.0/jquery.min"]
  48. // }
  49. // });
  50. // // 使用配置中的参数
  51. // // $必须实参传入
  52. // require(['jquery'],function ($) {
  53. // $(function () {
  54. // // 可以使用jQuery语法了
  55. // });
  56. // })
  57. // require.config配置路径,首先可以访问非本地的模块
  58. // 我们也可以把本地,模块也利用config来配置, 简化开发
  59. // 而且服务器的模块和本地的模块可以配合使用,
  60. // 比如服务器端的jQuery文件失效,就可以从本地访问jQuery,反之亦然.
  61. // 省略.js
  62. require.config({
  63. paths : {
  64. jquery : ["http://libs.baidu.com/jquery/1.9.0/jquery.min","jquery-2.2.3"],
  65. say : ["js/sayHello"],
  66. obj : ["js/obj"],
  67. person : ["js/Person"]
  68. }
  69. });
  70. // 使用配置中的参数
  71. // $必须实参传入
  72. // 没有返回值的放后面,say放最后
  73. require(['jquery','obj','person','say'],function ($,obj,Person) {
  74. $(function () {
  75. // 可以使用jQuery语法了
  76. alert("加载完成! ");
  77. alert(obj.name+"+++++"+obj.age);
  78. var xiaoming = new Person.Person("小雪",18);
  79. xiaoming.say();
  80. });
  81. })
  82. </script>
  83. <style type="text/css">
  84. .my_div{
  85. width: 300px;
  86. height: 300px;
  87. background: red;
  88. }
  89. </style>
  90. </head>
  91. <body>
  92. <div class="my_div">
  93. </div>
  94. </body>
  95. </html>
  1. // Person.js
  2. define(function () {
  3. var Person = function (name , age) {
  4. this.name = name;
  5. this.age = age;
  6. }
  7. Person.prototype.say = function() {
  8. alert("我叫: "+this.name+"年龄: "+this.age);
  9. };
  10. return {
  11. Person : Person
  12. }
  13. });
  1. // sayHello.js
  2. // function fn () {
  3. // alert("你好啊!!!");
  4. // }
  5. // fn();
  6. // 定义模块 : define是一个全局变量 (函数)
  7. define(function () {
  8. function fn () {
  9. alert("你好啊!!!");
  10. }
  11. fn();
  12. });
  1. // obj.js
  2. define(function () {
  3. return {
  4. name : "小雪",
  5. age : 18
  6. };
  7. });

深入理解属性和变量


  1. 一 JS中的变量和属性有共同点, 可以类比
  2. 1. 变量可以存放基本类型和引用类型;
  3. Eg .
  4. /*=======存放基本类型========*/
  5. var x1 = 123;
  6. var x2 = "I MISS U";
  7. /*=======存放引用类型(指针)========*/
  8. var x3 = [];
  9. var x4 = {};
  10. var x4 = function () {} // 就是传说中的函数表达式(匿名函数)
  11. 2. 属性也可以存放基本类型和引用类型的值
  12. Eg .
  13. /*=======存放基本类型========*/
  14. var obj = {}; // 一个空的字面量对象
  15. obj.x1 = 123;
  16. obj.x2 = "I MISS U";
  17. /*=======存放引用类型(指针)========*/
  18. obj.x3 = [];
  19. obj.x4 = {};
  20. obj.x5 = function () {}; // 此时就是"方法"
  21. 二: 属性的特点
  22. 属性是属于某个对象的, 通过"."或 "[]"语法来调用
  23. 同时, 属性实际是字符串类型的
  24. var prop1 = 123; // 一个变量初始化
  25. var testObj = {};
  26. testObj.prop1 = "测试"; // 变量名和属性一样??
  27. // 验证其实不一样
  28. console.log(testObj.prop1); // 测试
  29. console.log(prop1); // 123
  30. // 上面说明变量名和属性重名没有被覆盖. 其实没有任何关系
  31. // 其实本质属性是字符串
  32. console.log(testObj["prop1"]); // 测试
  33. // 说明属性确实是字符串
  34. //再来看老师下午所讲的, 就能解释通 m1 : m1 , 这么写是合理的
  35. var modal = (function () {
  36. var age =18;
  37. var m1 = function () {
  38. return "m1: " + age;
  39. };
  40. var m2 = function () {
  41. age++;
  42. return "m2: " + age;
  43. };
  44. return {
  45. //"m1" : m1(), // modal.m1 是属性
  46. // 把函数的指针作为value
  47. // key m1是属性, value m1是上面的变量保存的函数指针
  48. // key m1 实际是"m1"
  49. m1 : m1, // modal.m1() 是方法
  50. m2 : m2 // modal.m2() 是方法
  51. };
  52. })();

同源策略与JSONP


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>同源政策</title>
  6. <script type="text/javascript">
  7. /*
  8. 【同源政策】
  9. 1995年, 同源政策被引入到浏览器中, 目前所有的浏览器都适用这种政策.
  10. 同源政策最初的含义是: A页面的cookie, B页面不能读取到A页面的cookie, 除非这两个页面是"同源"
  11. 【同源必须保证"三"相同】
  12. 1. 协议相同
  13. 2. 域名相同
  14. 3. 端口相同
  15. eg. http://www.ilovedota.com/dir/index.html
  16. 协议: http://
  17. IP地址: 192.168.0.163
  18. 域名: www.baidu.com
  19. DNS: Domain Name System,域名系统
  20. 端口: port 端口是在域名后面用冒号 ":" 写
  21. 例如: :8080(默认端口,省略)
  22. 有get请求的完整网址 : http://wwww.liu.com:8080/dir/index.html?username="sdfasd"&password="*****"
  23. 端口可分为虚拟端口和物理端口,其中虚拟端口指计算机内部或交换机路由器内的端口,不可见。例如计算机中的80端口、21端口、23端口等。物理端口又称为接口,是可见端口,计算机背板的RJ45网口,交换机路由器集线器等RJ45端口
  24. 测试:
  25. 1. http://www.ilovedota.com/dir/index.html
  26. 2. http://www.ilovedota.com/other/other.html
  27. 1和2是同源的,因为协议相同, 域名(IP地址)相同, 端口相同(都是默认的:8080端口)
  28. 3. https://www.ilovedota.com/other/other.html // 不同源,协议不同
  29. 4. http://www.ilove.com/other/other.html // 不同源,域名不同
  30. 5. http://www.ilovedota.com:2024/other/other.html // 不同源,端口不同
  31. 【同源的意义】
  32. 保证用户信息的安全, 防止恶意网站窃取用户数据
  33. 【同源政策能限制的范围】
  34. 1. cookie 无法获取
  35. 2. DOM无法获取
  36. 3. ajax无法发送请求 (?为嘛要限制它)
  37. // 能发送请求访问数据就肯能窃取信息
  38. // 多个项目在多个服务器间存在信息请求交换怎么办??
  39. // 服务器端解决方案(了解)
  40. 1. 架设代理服务器
  41. 2. 浏览器请求同源服务器
  42. 3. 然后再让同源服务器去请求其他服务器
  43. 【JSONP】
  44. JSONP概念: JSONP是JSON的一种使用模式, 可以使用网站达到跨域的功能.
  45. 【JSONP的流程】
  46. 1、客户端访问: www.liu.com:8080/dir/index.html?jsoncallback=callbackFn
  47. 2、假设访问后想返回的数据是: {"name" : "liu", "age" : 18};
  48. 3、JSONP实际上返回的是callbackFn{"name" : "liu", "age" : 18}
  49. 返回的不是json格式的数据, 而是一个代码段
  50. 【JSONP的实现】
  51. 1、通过script中的 src="在这里传"
  52. 2、jQuery实现, 见demo
  53. 【CORS (了解)】
  54. 【WebSoket (了解)】
  55. */
  56. </script>
  57. </head>
  58. <body>
  59. <!--
  60. 【JSONP的流程】
  61. 1、客户端访问: www.liu.com:8080/dir/index.html?jsoncallback=callbackFn
  62. 2、假设访问后想返回的数据是: {"name" : "liu", "age" : 18};
  63. 3、JSONP实际上返回的是callbackFn{"name" : "liu", "age" : 18}
  64. 返回的不是json格式的数据, 而是一个代码段
  65. -->
  66. // 一. script的src实现
  67. // 支持JSONP就可以用
  68. <script>
  69. function callbackFn (data) {
  70. // 返回data
  71. // 可以进行解析
  72. }
  73. </script>
  74. <script type="text/javascript"></script>
  75. <script type="text/javascript" src="http://liu.com:8080/index.html?jsoncallback=callbackFn"></script>
  76. // 二. jQuery的实现
  77. <script>
  78. $.getJSON("get","http://liu.com:8080/index.html?jsoncallback=?",function (data) {
  79. // 进行解析
  80. });
  81. </script>
  82. </body>
  83. </html>

匿名函数和callback深入解析


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>匿名函数与callback深入思考</title>
  6. <script>
  7. // 函数的三大特点:
  8. // 声明, 实现, 调用
  9. // 练习callback
  10. // 其实就是把函数作为参数传递
  11. // 实际传的是函数的地址
  12. function fn (x,y) {
  13. console.log(x+y(x));
  14. }
  15. function fn1 (x) {
  16. if (x<10) {
  17. return "我是传了参数的回调函数" +x;
  18. } else{
  19. return "我是窜了参数的" + x;
  20. }
  21. }
  22. fn(2,fn1);
  23. // ajax中成功的回调函数
  24. // 在函数的内部整理出了结果, 传入回调函数中, 在回调函数内能使用
  25. //
  26. // 匿名函数的传参调用
  27. (function (x) {
  28. console.log("我是匿名函数传了参"+ x);
  29. })(100000);
  30. // 函数内部的回调函数
  31. // 模拟ajax
  32. // function test (x,successCallbackFn) {
  33. // x +="wo ";
  34. // x +="爱你";
  35. // x += "一万年";
  36. // successCallbackFn(x); // 这里的x是实参,也可以successCallbackFn("我这的就只是实参");
  37. // return "\"我是返回值猜猜看\"";
  38. // }
  39. // test(1314,function (y) {
  40. // console.log(y);
  41. // })
  42. // 等价于
  43. function test2 (x) {
  44. x +="wo ";
  45. x +="爱你";
  46. x += "一万年";
  47. // (function(x){
  48. // console.log(x);
  49. // })("调用里传的是实际的参数");
  50. (function(){
  51. console.log(arguments[0]);
  52. })("调用里传的是实际的参数");
  53. return "\"我是返回值猜猜看\"";
  54. }
  55. test2(1314);
  56. </script>
  57. </head>
  58. <body>
  59. </body>
  60. </html>














添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注