Day06 - Fetch、filter、正则表达式实现快速古诗匹配
Day06 - Fetch、filter、正则表达式实现快速古诗匹配
效果图在输入框中搜索
涉及特性
实现步骤
布局篇
<form class="search-form"> <input type="text" class="search" placeholder="诗人名字,关键字"> <ul class="suggestions"> <li>输入词句,找一首诗</li> <li>输入词句,找一首诗</li> <li>输入词句,找一首诗</li> <li>输入词句,找一首诗</li> <li>输入词句,找一首诗</li> </ul> </form>
html { box-sizing: border-box; margin: 0px; background-color: rgb(145,182,195); font-family: 'Kaiti','SimHei','Hiragino Sans GB ','helvetica neue'; font-size: 20px; font-weight: 200; } *,*:before,*:after { box-sizing: inherit; } body { display: flex; justify-content: center; } .search-form { max-width: 700px; display: flex; flex-direction: column; justify-content: center; align-items: center; } input.search { padding: 20px; font-family: 'Kaiti','helvetica neue'; margin: 0; border: 10px solid #f7f7f7; font-size: 40px; text-align: center; width: 120%; outline: 0; border-radius: 5px; position: relative; top: 10px; left: 10px; box-shadow: 0 0 5px rgba(0,0.12),inset 0 0 2px rgba(0,0.19); } .suggestions { margin: 0; padding: 0; position: relative; top: 7px; width: 100%; } .suggestions li { background: white; list-style: none; border-bottom: 1px solid #D8D8D8; box-shadow: 0 0 10px rgba(0,0.14); margin: 0; padding: 20px; display: flex; flex-direction: column; /*align-items: flex-start;*/ } span.title { margin-right: 20px; text-align: right; color: #7c8e94; margin-top: 5px; } span.hl { color: green; } /*偶数匹配*/ .suggestions li:nth-child(even) { transform: perspective(100px) rotateX(3deg) translateY(2px) scale(1.001); background: linear-gradient(to bottom,#ffffff 0%,#efefef 100%); } /*奇数匹配*/ .suggestions li:nth-child(odd) { transform: perspective(100px) rotateX(-3deg) translateY(3px); background: linear-gradient(to top,#efefef 100%); }
通过Fetch下载数据解析并且保存const endpoint = 'https://gist.githubusercontent.com/liyuechun/f00bb31fb8f46ee0a283a4d182f691b4/raw/3ea4b427917048cdc596b38b67b5ed664605b76d/TangPoetry.json'; const poetrys = []; fetch(endpoint) .then(blob => { return blob.json(); }) .then(data => { poetrys.push(...data); }); 具体数据请求过程见下图:
Fetch详细使用文档
事件监听const search = document.querySelector('.search'); const suggestions = document.querySelector('.suggestions'); search.addEventListener('change',displayMatches); search.addEventListener('keyup',displayMatches); 获取 数据匹配操作
RegExp参考文档
function findMatches(wordToMatch,poetrys) { return poetrys.filter(poet => { // 正则找出匹配的诗句 const regex = new RegExp(wordToMatch,'gi'); const author = poet.detail_author.join(''); // console.log(author); return poet.detail_text.match(regex) || poet.title.match(regex) || author.match(regex); }); } function displayMatches() { const matches = findMatches(this.value,poetrys); const regex = new RegExp(this.value,'gi'); const html = matches.map(poet => { // 替换高亮的标签 const text = poet.detail_text.replace(regex,`<span class="hl">${ this.value }</span>`); const title = poet.title.replace(regex,`<span class="hl">${ this.value }</span>`); const detail_author = poet.detail_author[0].replace(regex,`<span class="hl">${ this.value }</span>`); // 构造 HTML 值 return ` <li> <span class="poet">${ text }</span> <span class="title">${ title } - ${ detail_author }</span> </li> `; }).join(''); // console.log(html); suggestions.innerHTML = html; }
源码下载Github Source Code
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |