File tree Expand file tree Collapse file tree 3 files changed +68
-23
lines changed
Expand file tree Collapse file tree 3 files changed +68
-23
lines changed Original file line number Diff line number Diff line change 9898
9999 ```js
100100 document.documentElement.style.setProperty('--base', '#fff');
101- ```
102-
103-
104-
105-
106-
107-
108-
109-
110-
111-
112-
113-
114-
101+ ```
Original file line number Diff line number Diff line change 1- # 06 JS 实现快速匹配指南
1+ # 06 Fetch 结合 filter 实现快速匹配古诗
22
33> 作者:©[ 缉熙Soyaine] ( https://github.com/soyaine )
44> 简介:[ JavaScript30] ( https://javascript30.com ) 是 [ Wes Bos] ( https://github.com/wesbos ) 推出的一个 30 天挑战。项目免费提供了 30 个视频教程、30 个挑战的起始文档和 30 个挑战解决方案源代码。目的是帮助人们用纯 JavaScript 来写东西,不借助框架和库,也不使用编译器和引用。现在你看到的是这系列指南的第 6 篇。完整指南在 [ GitHub] ( https://github.com/soyaine/JavaScript30 ) ,喜欢请 Star 哦♪(^∇^* )
77
88![ 仿即时搜索诗句效果] ( https://cl.ly/0b360y270s0f/Screen%20recording%202016-12-31%20at%2010.05.23%20PM.gif )
99
10- 在输入框中输入一个词,迅速展示含有这个词的诗句。
10+ 在输入框中输入一个词,迅速匹配,展示含有这个词的诗句,诗句的来源 json 数据是加载页面时从网络中异步获得。(原课程中选的内容是英文城市名,我将其换成了唐诗,地址可在我的页面中获取)
11+
12+ 初始文档中提供了 HTML 和 CSS,我们需要补全 JS 代码。这个挑战是 Ajax 的预热练习,在异步方面用到了一些目前还未被完全支持的新特性,但很好用。
13+
14+ ## 涉及特性
15+
16+ - ` promise `
17+ - `fetch()`
18+ - `then()`
19+ - `json()`
20+ - Array
21+ - `filter()`
22+ - `push()`
23+ - `join()`
24+ - ` RegExp `
25+ - `match()`
26+ - `replace()`
27+
28+ ## 过程指南
29+
30+ 1 . 声明一个空数组,用于存放解析 json 后的数据
31+ 2 . 运用 ` fetch() ` 获取 json 文件
32+ 1. 获取 json 响应
33+ 2. 解析数据
34+ 3. 存入数组
35+ 3 . 获取两个主要 HTML 元素(` <input> ` ,` <ul> ` ),给 ` <input> ` 添加事件监听(` change ` , ` keyup ` )
36+ 4 . 编写匹配输入的函数
37+ 1. 运用 `filter()` 过滤数组数据
38+ 2. 创建正则表达式,构造过滤条件
39+ 5 . 编写展示匹配结果的函数
40+ 1. 获取匹配数据
41+ 2. 替换关键词放入高亮的标签
42+ 3. 构造 HTML 标签数据
43+ 4. 将匹配值的 HTML 标签放入 `<ul>` 中
44+
45+ ## 相关知识
46+
47+ ### [ Fetch API] ( https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API )
48+
49+ Fetch API 这个新特性,是 XMLHttpRequest 获取资源新的替代方案,目前还是一个实验中的功能,截至到 2017.01.01 在 MDN 显示的支持情况是:Chrome 42.0、Firefox (Gecko) 39、Opera 29、Chrome for Android 42.0、Android Webview。如何使用可以看[ 这篇文章] ( https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch ) 。
50+
51+ #### [ fetch()] ( https://developer.mozilla.org/zh-CN/docs/Web/API/GlobalFetch/fetch )
52+
53+ Fetch API 提供一个全局的方法 ` fetch() ` ,这个方法(至少)需要接受 ` 资源的路径 ` 作为参数,返回值是一个 Promise 对象。若请求成功,这个对象包含了 Request 对应的 Response,但这只是一个 HTTP 相应。
54+
55+ 语法如下:
56+
57+ ``` js
58+ fetch (input, init).then (function (response ) { ... });
59+ ```
60+
61+ ####
62+
63+ 在这个挑战的示例中,使用此方法的截图如下:
64+
65+ ![ fetch(url).then()] ( https://cl.ly/3P3F1F2y1510/Image%202017-01-01%20at%206.58.45%20PM.png )
66+
67+
68+
69+
70+
71+
72+ > 10:45 -
Original file line number Diff line number Diff line change 1717< script >
1818// const endpoint = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json';
1919
20- const endpoint = 'https://raw.githubusercontent.com/soyaine/FE-Practice/gh-pages /TangPoetryCut.json' ;
20+ const endpoint = 'https://raw.githubusercontent.com/soyaine/FE-Practice/f438d3bdf099461f88322b1b1f20c9d58f66f1ec /TangPoetryCut.json' ;
2121
2222
2323 const poetrys = [ ] ;
3030 // 找出匹配的诗句
3131 const regex = new RegExp ( wordToMatch , 'gi' ) ;
3232 let author = poet . detail_author . join ( '' ) ;
33- console . log ( author ) ;
33+ // console.log(author);
3434 return poet . detail_text . match ( regex ) || poet . title . match ( regex ) || author . match ( regex ) ;
3535 } ) ;
3636 }
6060 </li>
6161 ` ;
6262 } ) . join ( '' ) ;
63- // <span class="info">
64- // <span class="title">${ poet.title }</span>
65- // <span class="author">${ poet.detail_author[0] }</span>
66- // </span>
67- console . log ( html ) ;
63+ // console.log(html);
6864 suggestions . innerHTML = html ;
6965 }
7066
You can’t perform that action at this time.
0 commit comments